I have been looking this sample project http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html and I wanted to populate these page views with data taken from an RSS Feed. I have all of the data parsed, placed into an array in the delegate and ready to export into the pages at the proper indexes. I changed the dataPages into a NSMutableArray and am attempting to populate the dataPages array as follows (DataSource.m):
-(id) init {
self = [super init];
if (self != nil)
{
dataPages = [[NSMutableArray alloc] init];
PagesAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
for (int i = 0; i < [delegate.pageTitle count]; i++) {
[dataPages addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%@", [delegate.pageTitle objectAtIndex:i]], @"pageTitle",
[NSString stringWithFormat:@"%@",[delegate.pageText objectAtIndex:i]], @"pageText",
nil]];
}
return self;
}
-(NSInteger)dataPageNumber {
return [dataPages count];
}
-(NSDictionary *)dataForPage:(NSInteger)pageIndex {
return [dataPages objectAtIndex:pageIndex];
}
However using the DataSource class, the data is loaded into the pages before the parser in the delegate is able to populate the arrays in time. Is there a way to load the class after the delegate parser is completed? my app does have a delegate trigger function once the rssParser has completed its process. I’m using xCode 4 if that helps anything
Only initialize the required things in your init method since is the first thing that is called, like the dataPages mutableArray; then when your delegate get response, call a method that populates the array and the scrollView. Also, if you’re gonna do that, remember that UIKit objects are not thread safe, so if you’re gonna declare a method that populates the array and scrollView, call in on the main thread to prevent your app crashing.