cellForRowAtIndexPath called too late
I am trying to re-load a table every time some data I get from the web is available. This is what I have:
SearchDataViewController:- (void)parseDataXML { parsingDelegate = [[XMLParsingDelegate alloc] init]; parsingDelegate.searchDataController = self; // CONTAINS THE TABLE THAT NEEDS RE-LOADING; ImplementedSearc开发者_开发百科hViewController *searchController = [[ImplementedSearchViewController alloc] initWithNibName:@"ImplementedSearchView" bundle:nil]; ProjectAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; UINavigationController *nav = (UINavigationController *)[delegate.splitViewController.viewControllers objectAtIndex: 0]; NSArray *viewControllers = [[NSArray alloc] initWithObjects:nav, searchController, nil]; self.splitViewController.viewControllers = viewControllers; [viewControllers release]; // PASS A REFERENCE TO THE PARSING DELEGATE SO THAT IT CAN CALL reloadData on the table parsingDelegate.searchViewController = searchController; [searchController release]; // Build the url request used to fetch data ... NSURLRequest *dataURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]]; parsingDelegate.feedConnection = [[[NSURLConnection alloc] initWithRequest:dataURLRequest delegate:parsingDelegate] autorelease]; }
ImplementedSearchViewController:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"count = %d", [keys count]); // keys IS A NSMutableArray return [self.keys count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.textLabel.text = [keys objectAtIndex:row]; ... }
XMLParsingDelgate:-(void) updateSearchTable:(NSArray *)array { ... [self.currentParseBatch addObject:(NSString *)[array objectAtIndex:1]]; // RELOAD TABLE [self.searchViewController.table reloadData]; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"..."]) { self.currentParseBatch = [NSMutableArray array]; searchViewController.keys = self.currentParseBatch; ... } ... } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"..."]) { ... [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO]; } ... }
My problem is that when I debug, the calls go between reloadData and numberOfRowsInSection until the keys array is filled with the last data, time at which the cellForRowAtIndexPath gets called:
reloadData -> numberOfRowsInSection reloadData -> numberOfRowsInSection ... reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath
What I want is for the table to be updated for each element I send, one by one:
reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath ... reloadData -> numberOfRowsInSection -> cellForRowAtIndexPath
Any ideas why this behavior?
Thank you!
Are you asking how to load parts of the table view instad of the whole thing? If so then u can use relaodRowsAtINdexPath: or reloadSectionsAtIndexPath for this purpose, here is a reference TableView ref...
Changing from [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:NO];
to [self performSelectorOnMainThread:@selector(updateSearchTable:) withObject:array waitUntilDone:YES];
solved my problem.
The Problem is with your use of threads NOT with "just" turning a flag ON or Off.Perform the selector on a different thread and it would begin to work.You can also use performSelectorOnBackground
精彩评论