ios – 补充视图不被删除

我正在构建一个自定义的布局集合视图.我的收藏中每个项目有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:

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

解决方法

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

首先,在 – prepareForCollectionViewUpdates :,我列举更新,并确定哪些项目正在消失.我缓存该组索引路径.

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

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...