插入包含页脚的UICollectionView部分的问题 [英] Issues inserting into UICollectionView section which contains a footer

查看:242
本文介绍了插入包含页脚的UICollectionView部分的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个典型的UICollectionView,它以垂直方式使用UICollectionViewFlowLayout。我正在使用带有分页的rest API来填充集合视图。为了触发下一页下载,我在请求页脚布局时使用委托:

I've got a typical UICollectionView which is using UICollectionViewFlowLayout in a vertical fashion. I'm using a rest API with pagination to populate the collection view. In order to trigger the next page to download, I'm using the delegate when it asks for the footer layout:

- (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背后的代码:

Here is the code behind my 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查看资产的第一页加载。如果我向下滚动,我会看到页脚视图(包含一个微调器),但代码将在行的异常断点处中断:

When I run I can go to my ViewController and see the first page of assets loaded. If I scroll down I'll see the footer view (which contains a spinner), but then the code will break at the exception break point at line:

[weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];

断言失败 - [UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:],/ SourceCache / UIKit / UIKit-3185.20 / UICollectionViewData.m:829

Assertion failure in -[UICollectionViewData layoutAttributesForSupplementaryElementOfKind:atIndexPath:], /SourceCache/UIKit/UIKit-3185.20/UICollectionViewData.m:829

然后如果我继续它会因错误而崩溃:

Then if I continue it crashes with the error:

2014-06-11 16:39:58.335 Radius-iOS [4901:525006] *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'没有UICollectionViewLayoutAttributes实例用于-layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionFooter at path {length = 2,path = 0 - 0}'
*
第一次抛出调用堆栈:

2014-06-11 16:39:58.335 Radius-iOS[4901:525006] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionFooter at path {length = 2, path = 0 - 0}' * First throw call stack:

这令我感到困惑。 layoutAttributesForSupplementaryElementOfKind听起来像是与布局类有关,但我没有使用自定义流布局而是提供默认布局。听起来Apple的代码很疯狂,但我觉得我正在使用一切。

This is puzzling to me. layoutAttributesForSupplementaryElementOfKind sounds like it's seomthing to do with the layout class, but I'm not using a custom flow layout rather the default one supplied. It sounds like Apple's code is mad but I feel that I'm using everything correctly.

现在,如果我移动电话:

Now if I move the call:

[self loadNextPageOfAssets];

从补充单元格出列到UICollectionViewCell双端队列,并一起删除页脚视图,然后插入效果很好。

from the supplementary cell dequeue to teh UICollectionViewCell deque, and remove the footer views all together, then the insert works great.

现在我正在调用reloadData而不是insert,但这是UGLY。

For now I'm calling reloadData instead of insert, but this is UGLY.

我是否忽略了有关页脚的内容?

Am I overlooking something about the footers?

推荐答案

答案由VaporwareWolf提供的是一个黑客攻击。通过返回一个小尺寸而不是零尺寸,补充视图将始终存在但尺寸太小而无法看到。这就是为什么它修复 NSInternalInconsistencyException

The answer provided by VaporwareWolf is a bit of a hack. By returning a tiny size instead of a zero size, the supplementary view will always exist but at a size too small to see. So that's why it fixes the NSInternalInconsistencyException

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

将数据添加到数据源之后和
之后调用 insertItemsAtIndexPaths ,只是使集合视图上的布局无效,以使其了解更改。

After adding the data to the datasource and before calling insertItemsAtIndexPaths, just invalidate the layout on the collectionview to make it aware of the changes.

Objective-C

[self.collectionView.collectionViewLayout invalidateLayout]

Swift

collectionView.collectionViewLayout.invalidateLayout()

这篇关于插入包含页脚的UICollectionView部分的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆