How can I check to see whether an indexPath
is valid or not?
I want to scroll to an indexPath
, but I sometimes get an error if my UICollectionView
subviews aren't finished loading.
You could check
- numberOfSections
- numberOfItemsInSection:
of your UICollectionViewDataSource
to see if your indexPath is a valid one.
E.g.
extension UICollectionView {
func isValid(indexPath: IndexPath) -> Bool {
guard indexPath.section < numberOfSections,
indexPath.row < numberOfItems(inSection: indexPath.section)
else { return false }
return true
}
}