如何增加 UICollectionView 的 NSIndexPath

How to increment NSIndexPath of a UICollectionView

我正在调用此方法,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?

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

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

尝试使用类似这样的东西创建一个新的 NSIndexPath

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