xcode UICollectionView reloadItemsAtIndexPaths

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21083382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:32:34  来源:igfitidea点击:

UICollectionView reloadItemsAtIndexPaths

xcodepaginationuicollectionviewnsindexpath

提问by Jdizzle Foshizzle

I've been trying to wrap my head around the reloadItemsAtIndexPaths method of UICollectionView.

我一直试图围绕 UICollectionView 的 reloadItemsAtIndexPaths 方法。

I have an array of objects called objectsArray. When a user scrolls to the bottom of my collection view, I fetch the next batch of objects from the backend and append it to objectsArray by calling [objectsArray addObjectsFromArray:objects]; After doing so, I call [self.collectionView reloadData] which I know is expensive.

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

I'd like to optimize with the code below but I get an assertion failure when calling reloadItemsAtIndexPaths.

我想使用下面的代码进行优化,但在调用 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];
    }

Error: *Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:3716

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

Any help/tips is greatly appreciated.

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

Thanks!

谢谢!

回答by Dan Treiman

It looks like the items being added are new, in other words you are adding items which weren't there before. In that case, just replace reloadItemsAtIndexPaths:indexPaths with insertItemsAtIndexPaths:

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

[self.collectionView insertItemsAtIndexPaths:indexPaths]; // Use this for newly added rows
//[self.collectionView reloadItemsAtIndexPaths:indexPaths]; // Use this for existing rows which have changed

回答by Jdizzle Foshizzle

The assertion error was misleading. It wasn't until after I implemented

断言错误具有误导性。直到我实施之后

            @try
            {
                [self.collectionView insertItemsAtIndexPaths:indexPaths];
            }
            @catch (NSException *except)
            {
                NSLog(@"DEBUG: failure to insertItemsAtIndexPaths.  %@", except.description);
            }

Did I realize was calculating my indexPaths wrong. My index offset for the new objects was off by one making my collectionview think I was adding more objects than I specified in numberOfItemsInSection.

我是否意识到计算我的 indexPaths 是错误的。我的新对象的索引偏移量减少了一个,使我的 collectionview 认为我添加的对象比我在 numberOfItemsInSection 中指定的要多。

Thanks for all those who attempted to help!

感谢所有试图提供帮助的人!

回答by Boris

This error often arises when you increase/decrease the number of items in the array but it doesn't match the data source method

当您增加/减少数组中的项目数但与数据源方法不匹配时,通常会出现此错误

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Are you updating it appropriately?

你是否适当地更新它?