wait till UITableView finish reloadData [duplicate]

梦想与她 提交于 2019-12-21 02:35:11

问题


Possible Duplicate:
Get notified when UITableView has finished asking for data?

is it possible to get notification when tableview finish to reload its data after reloadData method? Or is it possible to wait till tableview finish reloading?

Thanks


回答1:


There is a very simple solution is to check if you are in the last iteration of the delegate

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

after last iteration finished in the end add your code there

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == lastRow){
          any code you want

    }
}

Thanks




回答2:


Lets put a completion block in. Who doesn't love a block?

@interface DUTableView : UITableView

   - (void) reloadDataWithCompletion:( void (^) (void) )completionBlock;

@end

and...

#import "DUTableView.h"

@implementation DUTableView

- (void) reloadDataWithCompletion:( void (^) (void) )completionBlock {
    [super reloadData];
    if(completionBlock) {
        completionBlock();
    }
}

@end

Usage:

[self.tableView reloadDataWithCompletion:^{
                                            //do your stuff here
                                        }];

EDIT:

Note that the above is only half the solution. The number of sections and rows and the row heights are updated synchronously on the main thread, so using the above code, the completion would call back when some of the UI part was done. But the data loading from the datasource is asynchronous - so really this answer isn't quite what @Michal wanted.



来源:https://stackoverflow.com/questions/6490884/wait-till-uitableview-finish-reloaddata

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