最近有个需求,需要获取tableView的刷新完成状态去做一些事情,找了好多方法,最终在http://stackoverflow.com/questions/16071503/how-to-tell-when-uitableview-has-completed-reloaddata这里找到了答案 。
两种方式 一种是
[self.tableView reloadData]; [self.tableView layoutIfNeeded];
// do sth
另外一种是
[self.tableView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ // do sth });
第一种方式是刷新的时候强制tableView 立即刷新,第二种是获取主线程队列,然后当tableView刷新完成之后会调用这个GCD的block。个人比较倾向于第二种方式,于是想探究一下原理。
之前有人说[tableView reloadData}是异步的,其实并不是,下面代码验证了我的说法
我在这里- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellID = @"cell";
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(!cell){
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSLog(@"cell index :%ld",(