ios - UITableView 在重新加载数据后不会立即显示单元格

标签 ios objective-c uitableview

在viewDidload中,tableview从服务器加载数据

success:^(NSArray *array) {
    if (!_dataSource) {
        _dataSource = [@[] mutableCopy];
    }
    if (array) {
        [_dataSource addObjectsFromArray:array];
        [_tableView reloadData];
    }
}

然后每次我从底部拉起,从服务器获取新数据并将其附加到_dataSource(与第一次相同的方式)

success:^(NSArray *array) {
    if (!_dataSource) {
        _dataSource = [@[] mutableCopy];
    }
    if (array) {
        [_dataSource addObjectsFromArray:array];
        [_tableView reloadData];
    }
}

现在问题来了:有时从底部拉起,成功获取新数据,但是tableview并没有立即显示新的cell,当我稍微扫一下tableview(甚至只是触摸屏幕)时,新的显示单元格。 可能是什么问题?

编辑

因为每个单元格的高度都不一样,所以我没有重用单元格,每次都初始化一个新的单元格,是这个原因吗?

//    TopicCell *cell = [tableView dequeueReusableCellWithIdentifier:TopicCellIdentifier];
//    if(!cell){    
//        cell = [[TopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TopicCellIdentifier];
//    }
TopicCell *cell = [[TopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TopicCellIdentifier];

@Jeffery Thomas,成功就是这样一个AFNetworking block

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 [manager GET:path
          parameters:parameters
             success:^(AFHTTPRequestOperation *operation1, id responseObject) {

             }
             failure:^(AFHTTPRequestOperation *operation2, NSError *error) {

             }];

最佳答案

作为第一个猜测,我会确保您在主线程(队列)上运行 -reloadData。这很简单。

success:^(NSArray *array) {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!_dataSource) {
            _dataSource = [@[] mutableCopy];
        }
        if (array) {
            [_dataSource addObjectsFromArray:array];
            [_tableView reloadData];
        }
    });
}

关于ios - UITableView 在重新加载数据后不会立即显示单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354354/

相关文章:

ios - 使用应用锁 MDM 配置文件检测 iOS 应用

iOS:viewWillDisappear 没有在从另一个 View Controller 动画的 popViewController 上被调用

iOS Metal 无法执行 'metal'(没有这样的文件或目录)

objective-c - UITextField onText 已更改

iphone - 不使用核心数据对 TableViewCell 进行编号

ios - 在 ContainerView 内的 UICollectionView 中计算单元格宽度

objective-c - 使用RestKit的编译错误

iphone - 水平重新定位 UIBarButtonItem

ios - 创建 Shazam 类型 UI ios Swift

ios - 当按下回车键时,UITableView 中的 UITextView 不会向上滚动