How can I check if an indexPath is valid, thus avoiding an “attempt to scroll to invalid index path” error?
问题 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. 回答1: You could check - numberOfSections - numberOfItemsInSection: of your UICollectionViewDataSource to see if your indexPath is a valid one. 回答2: A more concise solution? func indexPathIsValid(indexPath: NSIndexPath) -> Bool { if indexPath.section >= numberOfSectionsInCollectionView(collectionView) { return