Я получил эту ошибку на iOS 10, но она отлично работает как на iOS 11, так и на iOS 9.
* Завершение работы приложения из-за необработанного исключения 'NSRangeException', причина: '*- [__ NSArrayM objectAtIndex:]: индекс 5 за пределами [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)
}
Я проверил numberOfItems перед вызовом scrollToItem, новсе еще получил крах.Странно, кто-нибудь сталкивался с этой проблемой раньше?