UICollectionView 中的 ios NSIndexPath insertItemsAtIndexPaths

Posted

技术标签:

【中文标题】UICollectionView 中的 ios NSIndexPath insertItemsAtIndexPaths【英文标题】:ios NSIndexPath insertItemsAtIndexPaths in UICollectionView 【发布时间】:2014-03-25 21:14:27 【问题描述】:

我正在使用 insertItemsAtIndexPaths,但我试图了解此函数如何影响我的索引路径的排序。

我有 1 个部分和多个项目(或行)。我正在使用 UICollectionView。

当我插入新项目时,我使用带有 [NSIndexPath indexPathForItem:0 inSection:0] 的 insertItemsAtIndexPaths。我的问题是,当我在 indexPathForItem 0 处插入时,这个 collectionview 中已经存在的索引路径是否会发生变化?换句话说,假设我循环了 indexPathForItem:0。这实际上会在 UICollectionView 的开头推送项目吗?

【问题讨论】:

【参考方案1】:

根据documentation,您首先更新数据源,然后调用插入方法。因此,如果您的数据源是一个包含 3 个元素(例如 A、B、C)的数组,并且您在位置 0 处添加了一个新元素 D,那么您现在拥有 @[D、A、B、C]。然后你会调用 [self.collectionView insertItemsAtIndexPaths:@[[NSInexPath indexPathForItem:0 inSection:0]]];。

如果布局表明这会影响屏幕上的任何项目,则 cellForItemAtIndexPath 方法将被调用受影响的行,并且数据源将返回要在其中显示的内容。

【讨论】:

以上是关于UICollectionView 中的 ios NSIndexPath insertItemsAtIndexPaths的主要内容,如果未能解决你的问题,请参考以下文章

reloadData 不适用于 iOS7 中的 UICollectionView

UICollectionView IOS 中的标头操作

iOS7 应用管理器中的 UICollectionView “滑动”?

当 UICollectionView 位于 iOS 中的自定义 TableViewCell 中时,如何为 UICollectionView 实现委托和数据源方法,目标 C

ios 7 中的 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] 断言

iOS流布局UICollectionView系列七——三维中的球型布局