ios – 补充视图不被删除

前端之家收集整理的这篇文章主要介绍了ios – 补充视图不被删除前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个自定义的布局集合视图.我的收藏中每个项目有2个补充视图.而当我想删除一个项目时,我的layoutAttributesForSupplementaryViewOfKind:atIndexPath:实现被调用与不再存在的补充视图.这意味着我得到一个索引路径,它不能正确地映射到我的数据源,并且我有一个超出限制的问题. @H_403_2@我做了一些日志记录,在第一个实例中是什么跟随,是当我们从一些数据开始时,或者当我们将一个项目插入到集合视图中时,集合视图被计算的正确和逻辑的方式.

@H_403_2@第二组日志是当我删除一个对象从我的数据源deleteItemsAtIndexPaths:.

@H_403_2@我的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;
}
@H_403_2@初始日志(正确和逻辑):

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
@H_403_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
@H_403_2@正如你可以看到layoutAttributesForSupplementaryViewOfKind:atIndexPath:被调用,但是绕过layoutAttributesForElementsInRect:

@H_403_2@有谁知道会发生什么问题?

解决方法

我有同样的问题.我的布局为集合视图中的每个项目使用一个补充视图.我用几个步骤解决了这个问题. @H_403_2@首先,在 – prepareForCollectionViewUpdates :,我列举更新,并确定哪些项目正在消失.我缓存该组索引路径.

@H_403_2@然后布局将调用 – indexPathsToDeleteForSupplementaryViewOfKind :.我刚从上面返回缓存的结果.

@H_403_2@最后,在 – finalizeCollectionViewUpdates中,我清除缓存.我想这个步骤是可选的.

@H_403_2@我也这样做了 – indexPathsToInsertForSupplementaryViewOfKind:为了一致性,尽管它没有它工作正常.

猜你在找的iOS相关文章