iOS UITableView reloadData only refreshes table th

2019-06-23 02:31发布

问题:

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?

回答1:

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.



回答2:

I found out you are doing XML parsing by seeing that elementName in your if loop. Why you need to load a NIB file in your parsing code. What is the use of it? You can just call your reloadTable: method just like this.

[self reloadTable:self];

You are populating your tableView using some array in your app delegate file. So did you initialized your delegate in your ViewController?