如何判断indexPath
是否有效?
我想滚动到indexPath
,但如果我的UICollectionView
子视图没有完成加载,有时会出现错误.
如何判断indexPath
是否有效?
我想滚动到indexPath
,但如果我的UICollectionView
子视图没有完成加载,有时会出现错误.
你可以查一下
- numberOfSections
- numberOfItemsInSection:
查看您的indexPath是否有效.
例如.
extension UICollectionView {
func isValid(indexPath: IndexPath) -> Bool {
guard indexPath.section < numberOfSections,
indexPath.row < numberOfItems(inSection: indexPath.section)
else { return false }
return true
}
}