UICollectionView的延迟加载技术

Jac*_*tra 5 objective-c

我有一个IOS应用程序,它使用UICollectionView显示单元格的水平网格.我需要的只是当用户到达我的UICollectionView末尾时(懒惰)加载数据的一种聪明方式.我现在使用的代码是:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    GridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Cell forIndexPath:indexPath];


    /**
     TODO
     - Find a good way to check if the user has reach the end of the grid
     - This next piece of code is for demonstration purposes. A real check has to be
     done yet!!!
     **/

    // releases is my datasource //

    if(indexPath.row == (releases.count - 1)) {
        [self getNextListData];
    }

    return cell; // <-- finally return the cell


}
Run Code Online (Sandbox Code Playgroud)

这段代码的问题是:如果用户真的快速滚动到最后,他必须来回滚动才能加载更多项目.这实际上不是用户友好的,我需要一种更好的方法来在需要时加载项目.

有没有人有什么建议?

提前致谢.

Jef*_*mas 2

当我遇到表视图的这个问题时,我必须进行一些用户测试。我发现如果我在表格结束之前开始加载 10 个单元格,则新单元格应该准备就绪。也就是说,假设您正在后台加载数据。

if (indexPath.row >= [releases count] - 10)
    [self getNextListData];
Run Code Online (Sandbox Code Playgroud)

您可能需要进行一些测试,看看哪个数字适合您。

此外,我还有一个加载指示器单元。在部分回调中的行数中,我总是额外返回 1 行。

return [releases count] + 1;
Run Code Online (Sandbox Code Playgroud)

在行回调单元格的开头,我返回了一个特殊的加载指示器单元格。

if (indexPath.row == [releases count])
    return [JLTActivityCell sharedInstance];
Run Code Online (Sandbox Code Playgroud)

我不确定该建议将如何应用于集合视图,但希望它能为您指明一个好的方向。