我有一个典型的 UICollectionView,它以垂直方式使用 UICollectionViewFlowLayout。我正在使用带有分页的 REST API 来填充集合视图。为了触发下一页的下载,我在请求页脚布局时使用委托:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
RDLoadMoreReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"RDLoadMoreReusableView" forIndexPath:indexPath];
view.delegate = self;
[view start];
[self loadNextPageOfAssets];
return view;
}
这是我的 loadNextPageOfAssets 背后的代码:
-(void)loadNextPageOfAssets{
__weak RDRadiusGridViewController *weakSelf = self;
// Increment page and request assets. They are added to cluster.assets before the completion block is called.
self.cluster.pagination.page++;
[self.cluster requestAssetsWithCompletionBlock:^(NSArray *assets) {
SM_LOG_DEBUG(@"page.total: %ld assets.count: %ld", self.cluster.pagination.totalCount, (long)assets.count);
NSMutableArray *indexPaths = [[NSMutableArray alloc]initWithCapacity:assets.count];
for(NSUInteger index = 0; index < assets.count; index++){
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.cluster.assets.count - assets.count + index inSection:0];
SM_LOG_DEBUG(@"Inserting indexPath: %ld:%ld", (long)indexPath.item, (long)indexPath.section);
[indexPaths addObject:indexPath];
}
[weakSelf.collectionView performBatchUpdates:^{
[weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
} completion:^(BOOL finished) {
}];
// [weakSelf.collectionView reloadData];
}];
}
当我运行时,我可以转到我的 ViewController 并查看加载的资源的第一页。如果向下滚动,我将看到页脚视图(其中包含微调器),但是代码将在以下行的异常断点处中断:
[weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
-[UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:] 中的断言失败,/SourceCache/UIKit/UIKit-3185.20/UICollectionViewData.m:829
然后,如果我继续,它会崩溃并出现错误:
2014-06-11 16:39:58.335 Radius-iOS[4901:525006]* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-layoutAttributesForSupplementaryElementOfKind 没有 UICollectionViewLayoutAttributes 实例:路径 {length = 2,path = 0 - 0} 处的 UICollectionElementKindSectionFooter”
*首先抛出调用堆栈:
这让我很困惑。 layoutAttributesForSupplementaryElementOfKind 听起来像是与布局类有关,但我没有使用自定义流布局,而是使用提供的默认布局。听起来苹果的代码很疯狂,但我觉得我使用的一切都是正确的。
现在,如果我转移呼叫:
[self loadNextPageOfAssets];
从补充单元双端队列到 UICollectionViewCell 双端队列,并将页脚视图全部删除,然后插入效果很好。
现在我正在调用 reloadData 而不是 insert,但这很丑陋。
我是否忽略了页脚的某些内容?