我有一个典型的 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 处的 UICollectionElementKindSectionFooter - 0}' *首先抛出调用栈:
这让我很困惑。layoutAttributesForSupplementaryElementOfKind 听起来似乎与布局类有关,但我没有使用自定义流布局,而是提供的默认布局。听起来 Apple 的代码很疯狂,但我觉得我使用的一切都是正确的。
现在,如果我移动电话:
[self loadNextPageOfAssets];
从补充单元 dequeue 到 UICollectionViewCell deque,并一起删除页脚视图,然后插入效果很好。
现在我正在调用 reloadData 而不是插入,但这很丑陋。
我是否忽略了页脚的某些内容?