UICollectionView reloadItemsAtIndexPaths

Jdi*_*zle 4 xcode pagination nsindexpath uicollectionview

我一直试图绕过UICollectionView的reloadItemsAtIndexPaths方法.

我有一个名为objectsArray的对象数组.当用户滚动到我的集合视图的底部时,我从后端获取下一批对象,并通过调用[objectsArray addObjectsFromArray:objects]将其附加到objectsArray; 这样做后,我调用[self.collectionView reloadData],我知道这是昂贵的.

我想用下面的代码进行优化,但是在调用reloadItemsAtIndexPaths时我得到一个断言失败.

    if (self.searchPage == 0) {
        parseObjectsArray = [[NSMutableArray alloc] initWithArray:relevantDeals];
        [self.collectionView reloadData];

    } else {

        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
        for (int i = 0; i < [relevantDeals count]; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [indexPaths addObject:indexPath];
        }

        [parseObjectsArray addObjectsFromArray:relevantDeals];
        [self.collectionView reloadItemsAtIndexPaths:indexPaths];
        //[self.collectionView reloadData];
    }
Run Code Online (Sandbox Code Playgroud)

错误: *断言失败 - [UICollectionView _endItemAnimations],/ SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:3716

非常感谢任何帮助/提示.

谢谢!

小智 5

看起来添加的项目是新的,换句话说,您正在添加以前不存在的项目.在这种情况下,只需用insertItemsAtIndexPaths替换reloadItemsAtIndexPaths:indexPaths:

[self.collectionView insertItemsAtIndexPaths:indexPaths]; // Use this for newly added rows
//[self.collectionView reloadItemsAtIndexPaths:indexPaths]; // Use this for existing rows which have changed
Run Code Online (Sandbox Code Playgroud)