9

我有一个典型的 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 而不是插入,但这很丑陋。

我是否忽略了页脚的某些内容?

4

4 回答 4

10

VaporwareWolf 提供的答案有点骇人听闻。通过返回小尺寸而不是零尺寸,补充视图将始终存在,但尺寸太小而无法看到。这就是它修复NSInternalInconsistencyException的原因

但是,有一个真正的解决方案

将数据添加到数据源之后和 调用之前insertItemsAtIndexPaths,只需使 collectionview 上的布局无效以使其了解更改。

Objective-C

[self.collectionView.collectionViewLayout invalidateLayout]

迅速

collectionView.collectionViewLayout.invalidateLayout()
于 2016-04-05T21:21:55.920 回答
9

事实证明,我实际上遇到了布局问题(如错误描述所示)

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if(<myDecision>){
        return CGSizeZero;
    } else {
        return CGSizeMake(320, 50);
    }
}

CGSizeZero 是导致崩溃的原因。而是使用 CGSizeMake(0.001, 0.001)。这是唯一必要的改变。它现在按预期运行。

于 2014-08-15T18:47:16.297 回答
0

有一篇关于 UICollectionView 中错误的俄语文章:http: //habrahabr.ru/post/211144/

以下是该文章中的几种方法,可以解决您的问题:

@try {
    [self.collectionView insertItemsAtIndexPaths:indexPaths];
}
@catch (NSException *exception) {}

或者

[self.collectionView performBatchUpdates:^{
        [self.collectionView reloadData];
    } completion:nil];
于 2014-06-19T18:39:57.627 回答
0

您应该同时实现(页眉和页脚)方法。即使你只想要一个。看我的代码

-(UICollectionReusableView *) collectionView:(UICollectionView *) collectionView
           viewForSupplementaryElementOfKind:(NSString *) kind
                                 atIndexPath:(NSIndexPath *) indexPath
{
    UICollectionReusableView *header = [[UICollectionReusableView alloc] init];

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionViewHeader" forIndexPath:indexPath];
    }
    else {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionViewHeader" forIndexPath:indexPath];
    }

    return header;
}

-(CGSize) collectionView:(UICollectionView *) collectionView
                         layout:(UICollectionViewLayout *) collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger) section
{
    return CGSizeMake(self.view.frame.size.width, 100);
}

-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}
于 2014-08-14T19:58:12.343 回答