在我的集合中,有一个UICollectionView
实例,有25个单元。第一个单元位于indexPath(0,0),最后一个单元位于indexPath(0,24)。ScrollToItemAtIndexPath有时不起作用
不管我从第一个单元格滚动到最后一个单元格还是从最后一个单元格滚动到第一个单元格,目标单元格总是为零,这似乎是scrollToItemAtIndexPath
函数没有将单元格滚动到可见。
当我从第二个单元格滚动到第一个单元格时,它会很好。
我能做些什么才能在第一个和最后一个之间正确滚动? 我试过layoutIfNeeded
,但没什么不同。
这里是我的代码部分:
collection.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredVertically, animated: false)
print("indexPath")
print(indexPath)
guard let senderCell = collection.cellForItemAtIndexPath(indexPath) as? ImageCollectionViewCell else{
print(collection.cellForItemAtIndexPath(indexPath))
return
}
而且在安慰时,安慰:
indexPath
<NSIndexPath: 0x79e53110> {length = 2, path = 0 - 24}
nil
indexPath
<NSIndexPath: 0x7b8c2690> {length = 2, path = 0 - 0}
nil
indexPath
<NSIndexPath: 0x79fc48e0> {length = 2, path = 0 - 1}
indexPath
<NSIndexPath: 0x79eb3950> {length = 2, path = 0 - 0}
此解决方案效果很好。谢啦。不过,我不得不再次在viewDidLayoutSubviews中调用scrollToItemAtIndexPath,以便单元适合屏幕。对于没有这个的我来说,下一个单元格部分显示。 –