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

六眼飞鱼酱① 提交于 2019-12-23 10:06:37

问题


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?



来源:https://stackoverflow.com/questions/9831466/ios-uitableview-reloaddata-only-refreshes-table-the-second-time-i-call-my-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!