iphone - UITableView懒加载优化

标签 iphone objective-c ipad uitableview lazy-loading

我有 UITableView。在 tableView:cellForRow:atIndexPath: 方法中(当数据填充到单元格时)我实现了某种延迟加载。如果 rowData NSDictionary 中没有 key(key==row number) 对象,程序将在后台启动 requestDataForRow: 方法。因此在单元格变得可见后,单元格中的数据会被填充一点。 这是代码:

static int requestCounter=0;

-(void)requestDataForRow:(NSNumber *)rowIndex
{
    requestCounter++;
    //NSLog(@"requestDataForRow: %i", [rowIndex intValue]);
    PipeListHeavyCellData *cellData=[Database pipeListHeavyCellDataWithJobID:self.jobID andDatabaseIndex:rowIndex];
    [rowData setObject:cellData forKey:[NSString stringWithFormat:@"%i", [rowIndex intValue]]];
    requestCounter--;

    NSLog(@"cellData.number: %@", cellData.number);

    if (requestCounter==0)
    {
        //NSLog(@"reloading pipe table view...");
        [self.pipeTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    };
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"pipeCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    [[NSBundle mainBundle] loadNibNamed:@"PipesForJobCell" owner:self options:nil];
    cell = pipeCell;
    self.pipeCell = nil;


    PipeListHeavyCellData *cellData=[[PipeListHeavyCellData alloc] init];

    if ([rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]]==nil)
    {
        //NSLog(@"        nil data for row: %i", indexPath.row);
        [self performSelectorInBackground:@selector(requestDataForRow:) withObject:[NSNumber numberWithInt:indexPath.row]];
    }
    else
    {
        //NSLog(@"        has data for row: %i", indexPath.row);
        PipeListHeavyCellData *heavyData=[[PipeListHeavyCellData alloc] init];
        heavyData=(PipeListHeavyCellData *)[rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];
        cellData._id=[heavyData._id copy];
        cellData.number=[heavyData.number copy];
        cellData.status=[heavyData.status copy];
};

此代码有效,一切正常,但是我的表有 2000 行,如果用户非常快速地从索引为 10 的单元格滚动到索引为 2000 的单元格。他必须等待很长时间,直到所有拉取数据请求完成(对于第 11、12、13、...、2000 行),因为当用户滚动 TableView 时行变得可见,所以方法 requestDataForRow 被要求为他们服务。

我该如何优化这些东西?

最佳答案

我不得不做类似的事情。您需要创建一个队列来首先处理最近添加的项目。

比如用户打开表,有10个请求在排队。您使第一个对象出列并开始获取第一行的数据。然而,用户然后向下滚动到第 31-40 行。然后,您必须在队列中的前 10 行之前插入这些行,因为它们现在具有更高的优先级。关键是你不会立即立即启动10个请求,而是按顺序处理它们。这样,当用户滚动时,您只会“浪费”一个请求 - 最后一个请求。

实际实现它的一个简单方法是使用 [tableView indexPathsForVisibleRows]

关于iphone - UITableView懒加载优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10603302/

相关文章:

iphone - 使用 GMSReverseGeocodeResponse 解析地址时出现错误 com.google.HTTPStatus 代码 400

ios - iphone 中调用电话中的callID 和地址簿中的记录id 是否相同?

objective-c - 在 10.6 中的帮助查看器中启动帮助页面

ios - 如何获取WebView中每个请求的服务器响应代码

iphone - 我可以设置 SSO Facebook 屏幕的语言吗?

iphone - 如何为通用应用程序使用默认启动画面?

iphone - 从 CVPixelBuffer Reference 获取所需数据

ios - 基于 Swift NSLayoutConstraint 方程的常量

objective-c - objective-c : problem with directory file

iphone - 是否可以在 iPhone/iPad 上创建全屏 Web 应用程序,而无需先将其添加到主屏幕?