cellForRowAtIndexPath called too late

Posted by Mihai Fonoage on Stack Overflow See other posts from Stack Overflow or by Mihai Fonoage
Published on 2010-06-17T16:45:07Z Indexed on 2010/06/17 16:53 UTC
Read the original article Hit count: 232

Filed under:
|
|

Hi,

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)parseDatatXML {
    parsingDelegate = [[XMLParsingDelegate alloc] init];    
    parsingDelegate.searchDataController = self;
    // CONTAINS THE TABLE THAT NEEDS RE-LOADING;    
    ImplementedSearchViewController *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. I wanted the table to be updated for each element I send, one by one, instead of just in the end.

Any ideas why this behavior?

Thank you!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uitableview