补充意见未被删除 [英] Supplementary Views not being deleted

查看:77
本文介绍了补充意见未被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建自定义布局集合视图。我的收藏中每个项目有2个补充视图。当我想删除一个项目时,我的 layoutAttributesForSupplementaryViewOfKind:atIndexPath:实现将使用不再存在的补充视图进行调用。这意味着我得到的索引路径没有正确映射到我的数据源,而且我遇到了一个越​​界问题。

I am building a custom layout collection view. I have 2 Supplementary Views per item in my collection. And when I want to delete an item, my layoutAttributesForSupplementaryViewOfKind:atIndexPath: implementation is called with Supplementary Views that should no longer exist. This means I'm getting an index path which doesn't map to my data source properly and I get an out of bounds problem.

我做了一些日志记录,第一个例子中的内容是当我们开始一些数据时,或者每当我们插入一个项目时计算集合视图的正确和逻辑方式进入集合视图。

I did some logging and what follows in the first instance is the correct and logical way the collection view is computed when we begin with some data, or whenever we insert an item into the collection view.

第二组日志是从我的数据源删除对象后的 deleteItemsAtIndexPaths:

The second set of logs is when I deleteItemsAtIndexPaths: after deleting an object from my data source.

我的 layoutAttributesForElementsInRect:如下所示:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray* attributes = [NSMutableArray array];

    for (NSInteger i = 0 ; i < [self.myData count]; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

        NSLog(@"%@ indexPath.row:%d", NSStringFromSelector(_cmd), indexPath.row);

        UICollectionViewLayoutAttributes *att = [self layoutAttributesForItemAtIndexPath:indexPath];
        [attributes addObject:att];

        UICollectionViewLayoutAttributes *att_supp = [self layoutAttributesForSupplementaryViewOfKind:@"one_kind" atIndexPath:indexPath];
        [attributes addObject:att_supp];

        UICollectionViewLayoutAttributes *linesAttr = [self layoutAttributesForSupplementaryViewOfKind:@"another_kind" atIndexPath:indexPath];
        [attributes addObject:linesAttr];
    }
    return attributes;
}

初始日志(正确且合乎逻辑):

Initial logs (correct and logical):

MyApp[18546:70b] layoutAttributesForElementsInRect: indexPath.row:0
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18546:70b] layoutAttributesForElementsInRect: indexPath.row:1
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18546:70b] layoutAttributesForElementsInRect: indexPath.row:2
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:2
MyApp[18546:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:2

删除项目后:

MyApp[18633:70b] layoutAttributesForElementsInRect: indexPath.row:0
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:0
MyApp[18633:70b] layoutAttributesForElementsInRect: indexPath.row:1
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:1
MyApp[18633:70b] layoutAttributesForSupplementaryViewOfKind:atIndexPath: indexPath.row:2

当你可以看到 layoutAttributesForSupplementaryViewOfKind:atIndexPath:被调用但绕过 layoutAttributesForElementsInRect:

As you can see layoutAttributesForSupplementaryViewOfKind:atIndexPath: is called but by bypassing layoutAttributesForElementsInRect:

有谁知道可能出现什么问题?

Does anyone know what might be going wrong?

推荐答案

我遇到了同样的问题。我的布局为集合视图中的每个项目使用一个补充视图。我通过几个步骤解决了这个问题。

I had this same issue. My layout uses one supplementary view for every item in the collection view. I solved it with a couple of steps.

首先,在 - prepareForCollectionViewUpdates:中,我列举了更新和确定哪些项目正在消失。我缓存了那组索引路径。

First, in - prepareForCollectionViewUpdates:, I enumerate over the updates and determine which items are disappearing. I cache that set of index paths.

然后布局将调用 - indexPathsToDeleteForSupplementaryViewOfKind:。我刚刚从上面返回了缓存的结果。

The layout will then call - indexPathsToDeleteForSupplementaryViewOfKind:. I just returned the cached result from above.

最后,在 - finalizeCollectionViewUpdates 中,我清除了缓存。我想这一步是可选的。

Finally, in - finalizeCollectionViewUpdates, I clear the cache. I suppose this step is optional.

我也为做了这个 - indexPathsToInsertForSupplementaryViewOfKind:为了保持一致,尽管它有效没有它就好了。

I also did this for - indexPathsToInsertForSupplementaryViewOfKind: for consistency sake, although it worked just fine without it.

这篇关于补充意见未被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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