【问题标题】:no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind:-layoutAttributesForSupplementaryElementOfKind 没有 UICollectionViewLayoutAttributes 实例:
【发布时间】:2016-06-24 04:31:48
【问题描述】:

标题是我得到的错误,我不知道为什么,但这里有一些信息,希望这里的人可以解释我。

我已对UICollectionViewFlowLayout 进行了子类化,因为这样可以节省我自己在prepareLayout 中计算单元格的帧(也许这是一个问题?)。然后我使用UICollectionViewLayoutAttributes 信息计算覆盖它的补充视图,得到我想要的布局。

我使用performBatchUpdates:completion: 来添加、删除和更新视图。插入工作正常,但删除项目时出现标题中显示的错误。

所以我知道为什么会发生错误,但我不知道为什么会发生。举例说明导致问题的场景

  1. 从 1 个项目和 1 个补充视图 1 个部分开始
  2. 再添加两个项目(prepareLayout 看到 3 个项目和 3 个补充视图)
  3. 删除项目(prepareLayout 看到 2 个视图和 2 个补充视图)
  4. layoutAttributesForSupplementaryViewOfKind:atIndexPath: 被称为请求具有 section:0 和 item:2 的索引路径的属性
  5. 崩溃,因为它要求第三个补充视图的属性,即使之前它调用了准备布局设置 2 个项目和 2 个补充视图
  6. 在无奈和绝望中举手

所以据我所知,有问题的功能是:

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
    return self.layoutInfo[elementKind][indexPath];
}

这当然是由UICollectionView 的内部网络自动调用的,所以我不知道为什么它在那个索引路径上要求那个补充视图。

有人有什么想法吗?也许这就是我使用performBatchUpdates:completion: 的方式,但删除工作正常,直到添加补充视图。我可以根据需要提供更多代码/解释。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewlayout uicollectionreusableview


    【解决方案1】:

    我搜索了论坛寻找答案,并提出了一些建议。他们都没有给我我需要的帮助,最终为了赶上最后期限,我完全放弃了使用补充意见。

    几周后,出于好奇,我再次环顾四周,最终发现came across the following post,现在我又开始使用补充视图了。

    所以,别忘了返回您的:

    - (NSArray<NSIndexPath *> *)indexPathsToDeleteForSupplementaryViewOfKind:(NSString *)elementKind
    {
        return self.removedIndexPaths;
    }
    

    到您的集合视图布局。

    【讨论】:

    • @micromanc3r 这些是忍受问题的人的话,不知道为什么连续几天尝试随机的东西没有结果。很高兴我能帮助结束痛苦。
    【解决方案2】:

    为了防止崩溃,您可以为所有不再有效的 indexPaths 返回虚拟属性。这样的事情可以帮助防止你的崩溃:

    UICollectionViewLayoutAttributes *layoutAttributes = self.layoutInfo[elementKind][indexPath]; // add some safety checks if this access creates an out of bounds issue
    
    // create dummy layoutAttributes
    // the workaround
    if (layoutAttributes == nil) {
        UICollectionViewLayoutAttributes *dummyLayoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
        dummyLayoutAttributes.frame = CGRectZero;
        dummyLayoutAttributes.hidden = YES;
        layoutAttributes = dummyLayoutAttributes;
    }
    
    return layoutAttributes;
    

    这仍然会导致视图堆栈中的对象不应该存在,但它们被隐藏并且不会造成崩溃。下次UICollectionView 更新它的布局时,它应该清除旧的隐藏视图。

    【讨论】:

    • 从哪里获得 self.layoutInfo?
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多