-layoutAttributesForSupplementaryElementOfKind 没有 UICollectionViewLayoutAttributes 实例: [英] no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind:

本文介绍了-layoutAttributesForSupplementaryElementOfKind 没有 UICollectionViewLayoutAttributes 实例:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The title is the error I'm getting and I have no idea why, but here is some information so hopefully someone on here can elucidate me.

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

I have subclassed UICollectionViewFlowLayout as this saves me calculating the frames for the cell's myself (perhaps this is an issue?) in prepareLayout. I then use the UICollectionViewLayoutAttributes information to calculate a supplementary view that over lays it, I get the layout I desire.

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

I use performBatchUpdates:completion: to add,remove and update the view. Insertion works fine, however deleting items is when the error shown in the title appears.

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

So I know why the error is happening but i don't know why it should even be happening. To clarify with an example going through a scenario that causes the issue

  1. 从 1 个项目和 1 个补充视图 1 个部分开始
  2. 再添加两个项目(prepareLayout 看到 3 个项目和 3 个补充视图)
  3. 删除项目(prepareLayout 看到 2 个视图和 2 个补充视图)
  4. layoutAttributesForSupplementaryViewOfKind:atIndexPath: 被称为请求索引路径的属性 section:0 和 item:2
  5. 崩溃,因为它要求第三个补充视图的属性,即使之前它调用了准备布局设置 2 个项目和 2 个补充视图
  6. 在无奈和绝望中举手
  1. Start with 1 item with 1 supplementary view 1 section
  2. Add two more items (prepareLayout sees 3 items with 3 supplementary views)
  3. Delete item (prepareLayout sees 2 views with 2 supplementary views)
  4. layoutAttributesForSupplementaryViewOfKind:atIndexPath: is called asking for attributes for index path with section:0 and item:2
  5. Crash because it asked for attributes for a third supplementary view even though earlier it called prepare layout setting up 2 items and 2 supplementary views
  6. Throw hands up in resignation and despair

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

so the offending function as far as i can tell is:

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

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

which of course is called automagically by the internal networking of UICollectionView so I have no clue as to why it's asking for that supplementary view at that index path.

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

Anyone got any ideas? Perhaps it's how i use performBatchUpdates:completion: but deletion worked fine until adding supplementary views. I can provide more code/explanation as necessary.

推荐答案

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

I scoured the forums looking for an answer and came across a few suggestions. None of them gave me the succour I needed, and eventually in the interest of meeting deadlines, I dropped the use of supplementary views altogether.

几周后,出于好奇,我再次环顾四周,最终看到了以下帖子 现在我又开始使用补充视图了.

A few weeks later out of curiosity I looked around again and eventually came across the following post and now I'm back to using supplementary views again.

所以,别忘了返回您的:

So, don't forget to return your:

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

到您的集合视图布局.

这篇关于-layoutAttributesForSupplementaryElementOfKind 没有 UICollectionViewLayoutAttributes 实例:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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