iOS UITableView reloadData only refreshes table the second time I call my reload function

Daniel Chen picture Daniel Chen · Mar 22, 2012 · Viewed 8k times · Source

Here is my reload function

- (void) reloadTable:(id)sender
{   
    NSLog(@"Reload Table");
    NSLog(@"%@",[appDelegate queryList]);
    [self.driverTable reloadData];
    [self.driverTable setNeedsLayout];
    [self.driverTable setNeedsDisplay];
}

Here is where I call it after receiving data from the webservice

if( [elementName isEqualToString:@"Response"]) {
    Response = NO;
    LookupView *lookupView = [[LookupView alloc] initWithNibName:@"LookupView" bundle:nil];
    [lookupView reloadTable:self];
}

Problem I have right now is after I do a search by pressing a find button and receive the data. The table does not refresh with the new data. If I call the same function again by pressing the find button again the table reloads. Now I know the data is there because I print the array out before I call the table reload.

Any ideas why?

Answer

bobnoble picture bobnoble · Mar 23, 2012

I have run into the same issue. Per Apple's documentation, reloadData "should not be called in the methods that insert or delete rows." A workaround I have used is to delay the call to reloadData so that it is not immediately after updating table. Try this:

[self performSelector:@selector(delayedReloadData) withObject:nil afterDelay:0.5];

at the appropriate place with the following implementation of delayReloadData

 -(void)delayedReloadData{
    [self.tableView reloadData];
}

A negative side affect I have see from this approach is that if the table row is visible there is a short delay before it shows its content.