补充意见未被删除

时间:2013-11-14 11:24:51

标签: ios objective-c uicollectionview

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

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

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

我的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;
}

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

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:

有谁知道可能出现的问题?

1 个答案:

答案 0 :(得分:13)

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

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

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

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

为了一致起见,我也为- indexPathsToInsertForSupplementaryViewOfKind:做了这个,虽然没有它就可以正常工作。