UITableView 延迟加载单元格数据

Ole*_*leg 5 iphone objective-c uitableview

我需要在 UITableView 中实现某种延迟加载。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //... some initializations here

    //this is a function that pulls my cell data using helper class
    NSData *cellData=[MyDataClass dataForCellAtIndexPath:indexpath.row];

    //... here I populate pulled data to cell

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,表视图滚动不顺畅,因为dataForCellAtIndexPath方法很慢。所以我需要通过调用此方法来实现将数据惰性填充到单元格中。我期望的结果是表格视图将平滑滚动,但单元格的内容将在绘制单元格后填充一点。请帮助我,该怎么办?

Gre*_*ons 4

是的,您可以考虑将数据收集推送到后台线程上。首先要尝试的是最简单的选项,看看它是否能改进任何东西:

[MyDataClass performSelectorInBackground:@selector(dataForCellAtIndexPath:) withObject:indexpath.row];
Run Code Online (Sandbox Code Playgroud)

这篇文章提到了其他一些更复杂的可自定义选项,例如Grand Central DispatchNSThread