Xcode UICollectionView重新加载项满足扩展路径
我一直在试图对UICollectionView的ReloadItemsAndExpaths方法进行思考 我有一个名为objectsArray的对象数组。当用户滚动到集合视图的底部时,我从后端获取下一批对象,并通过调用[objectsArray addObjectsFromArray:objects]将其附加到objectsArray;这样做之后,我调用[self.collectionView reloadData],我知道这很昂贵 我希望使用下面的代码进行优化,但在调用ReloadItemSatinDexpath时,断言失败Xcode UICollectionView重新加载项满足扩展路径,xcode,pagination,uicollectionview,nsindexpath,Xcode,Pagination,Uicollectionview,Nsindexpath,我一直在试图对UICollectionView的ReloadItemsAndExpaths方法进行思考 我有一个名为objectsArray的对象数组。当用户滚动到集合视图的底部时,我从后端获取下一批对象,并通过调用[objectsArray addObjectsFromArray:objects]将其附加到objectsArray;这样做之后,我调用[self.collectionView reloadData],我知道这很昂贵 我希望使用下面的代码进行优化,但在调用ReloadItemSat
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];
}
if(self.searchPage==0){
parseObjectsArray=[[NSMutableArray alloc]initWithArray:relevantDeals];
[self.collectionView-reloadData];
}否则{
NSMutableArray*indexPaths=[[NSMutableArray alloc]init];
对于(int i=0;i<[相关交易计数];i++){
NSIndexPath*indexPath=[NSIndexPath indexPathForItem:i第1节:0];
[indexPaths addObject:indexPath];
}
[parseObjectsArray addObjectsFromArray:相关交易];
[self.collectionView重新加载ItemSatinExpaths:indexPaths];
//[self.collectionView-reloadData];
}
错误:
*-[UICollectionView _endItemAnimations],/SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:3716中的断言失败
非常感谢任何帮助/提示
谢谢 当您增加/减少数组中的项数但与数据源方法不匹配时,通常会出现此错误
-(NSInteger)tableView:(UITableView*)tableView行数section:(NSInteger)section
您是否适当地更新了它?看起来添加的项目是新的,换句话说,您添加的是以前没有的项目。在这种情况下,只需将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
断言错误具有误导性。直到我实施之后
@try
{
[self.collectionView insertItemsAtIndexPaths:indexPaths];
}
@catch (NSException *except)
{
NSLog(@"DEBUG: failure to insertItemsAtIndexPaths. %@", except.description);
}
我是否意识到我的指数计算错了。新对象的索引偏移量减少了1,这使得我的collectionview认为我添加的对象比我在numberOfItemsInSection中指定的多
感谢所有试图帮助的人 我也尝试过InsertItemSatinExpaths方法,最后得到了上面相同的断言。谢谢你的建议(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section{return[parseObjectsArray count];}它看起来像是在断言之前被调用的,因为我记录了[parseObjectsArray count]的值,并且在数组中添加新对象时返回更高的值。谢谢你的建议!