记录一下蛋疼的bug.
我用AFNetworking 做网络请求,回调成功后 执行[self.tableView reloadData] 二次刷新界面; 但是numberOfRows走,cellForRowAtIndexPath 方法却不走了.
stackOverFlow上说在主线程刷新界面就好了,这样:
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
我这样写程序直接假死了.
之后继续尝试以下方法, 依然无法刷新:
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationF
ade];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
甚至拿到cell直接赋值都不行.
ChatListCell *cell = (ChatListCell *)[self.tableViewcellForRowAtIndexPath:indexPath];
if (model.avatar)
{
cell.imageURL = kImageURLWith(model.avatar);
}
if (model.user_nicename)
{
cell.name = model.user_nicename;
}
一度无语,觉得只能用调[self viewDidLoad] 刷新界面了. 同时猜测可能是self.tableview的创建有问题.
本来的tableview是通过懒加载创建的的:
- (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
然后 [self.view addSubview:self.tableview] 添加到视图上;
最后尝试了下改成下边的就好了,创建视图的时候就添加.原因未知,其他地方保持上边的写法并没有问题:
- (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
return _tableView;
}