这曾经在 iOS 6 中工作,但在 iOS 7 中失败
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
NSArray *selectedPaths = self.collectionView.indexPathsForSelectedItems;
NSAssert(selectedPaths.count == 1, @"viewWillAppear - Should be 1 but is %d", selectedPaths.count);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *selectedPaths = collectionView.indexPathsForSelectedItems;
NSAssert(selectedPaths.count == 1, @"cellForItemAtIndexPath - Should be 1 but is %d", selectedPaths.count);
}
cellForItemAtIndexPath 中的断言失败:
*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“cellForItemAtIndexPath - 应为 1 但为 0”
我使用 selectItemAtIndexPath:indexPath API 在 viewWillAppear 中选择了一个项目,但是当调用 cellForItemAtIndexPath 时,collectionView 已经忘记选择了任何内容。知道为什么吗?