yothenberg
yothenberg

Reputation: 1178

Why does UICollectionView.indexPathsForSelectedItems forget I selected an item in viewWillAppear?

This used to work in iOS 6 but is failing in 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);
}

The assertion in cellForItemAtIndexPath fails:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'cellForItemAtIndexPath - Should be 1 but is 0'

I select an item in viewWillAppear with the selectItemAtIndexPath:indexPath API but by the time cellForItemAtIndexPath is called the collectionView has forgotten that anything was selected. Any idea why?

Upvotes: 0

Views: 2033

Answers (1)

yothenberg
yothenberg

Reputation: 1178

Doh!

UICollectionViewController.clearsSelectionOnViewWillAppear

Upvotes: 3

Related Questions