【问题标题】:How to increment NSIndexPath of a UICollectionView如何增加 UICollectionView 的 NSIndexPath
【发布时间】:2016-04-14 16:16:46
【问题描述】:

我正在调用此方法,并且 nextCellIndexPath 是大于当前单元格的 indexPath,但 indexPath 没有正确递增。

这里是 cellForItemAtIndexPath:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! ImageCollectionViewCell
    cell.nextCellIndexPath = indexPath.indexPathByAddingIndex(1)
    return cell
}

当我尝试滚动到下一个 indexPath 时,indexPath 似乎没有增加。

那么如何正确获取下一个 indexPath?

【问题讨论】:

  • 看起来您将nextCell 分配为NSIndexPath 实例,而不是单元格,对吗?
  • 正确。我可能应该重命名它。
  • 调用滚动时该项目是否已经在屏幕上可见?我认为它只会在项目不完全可见时滚动。

标签: ios swift uicollectionview nsindexpath


【解决方案1】:

此代码:indexPath.indexPathByAddingIndex(1) 未达到您的预期。这将为 indexPath 添加一个索引,而不是移动到下一行。

尝试使用类似的方法创建一个新的 NSIndexPath

let nextIndexPath:NSIndexPath = NSIndexPath(forRow: indexPath.row + 1, inSection: indexPath.section)

【讨论】:

    【解决方案2】:

    就出队可重用概念而言,这段代码看起来很危险。您将 indexPath 对象分配给可重用单元格的属性,您无法确定,当您尝试从 rightBumperTapped 方法访问它时,此 indexPath 对单元格有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多