【发布时间】:2019-04-01 17:19:01
【问题描述】:
我在 iOS 10 上遇到此错误,但在 iOS 11 和 iOS 9 上都可以正常工作。
* 由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'* -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
这是我的代码:
let indexPath = IndexPath(row: index, section: 0)
print(indexPath.row) // 5
//here is the problem. I got 6 items in my datasource but looks like collectionView still consider it's only 5.
print(collectionView.numberOfItems(inSection: 0)) // 6
if indexPath.row < collectionView.numberOfItems(inSection: 0) {
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
我在调用 scrollToItem 之前检查了 numberOfItems,但仍然崩溃。有点奇怪,有人遇到过这个问题吗?
【问题讨论】:
标签: crash