4

I don't know why this doesn't work. indexOfIcon is correct, section is correct (checked with NSLog) If I select one everything is correct. But this line doesn't do a thing...why? If selected it should have a blue border. This works great while doing it "manually" but not with code..

- (void)viewWillAppear:(BOOL)animated
{
    NSUInteger indexOfIcon;
    if(self.mainCategory.icon){
        indexOfIcon = [self.icons indexOfObject: self.mainCategory.icon];
    } else {
        indexOfIcon = 0;
    }
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom];
}
MichiZH
  • 5,587
  • 12
  • 41
  • 81
  • Make ensure Allows selection property is set to YES and set delegate properly and make sure after selecting item You are not reloading the collection View – Dinesh Kaushik Dec 13 '13 at 10:18

2 Answers2

10

add

    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.selected = YES;

and the cell will be selected.

The command [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom]; tells the collectionView, that the cell is in selected state, but don't set the state of the cell.

zeiteisen
  • 7,078
  • 5
  • 50
  • 68
  • I have it changed now to this, but doesn't work either: NSIndexPath *iconIndexPath = [NSIndexPath indexPathForRow:indexOfIcon inSection:0]; [self.collectionView selectItemAtIndexPath:iconIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionBottom]; UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:iconIndexPath]; cell.selected = YES; – MichiZH Dec 13 '13 at 10:18
  • 1
    make sure, that the collectionview has finished loading the cells. To test this, make in your viewWillAppear a [self performSelector:@selector(test) withObject:nil afterDelay:2]; and in the test method your code to select the cell. – zeiteisen Dec 13 '13 at 10:36
  • You're awesome! Obviously it didn't load the cells yet. But how can I solve this without this delay? Right now you are in the collection view and after 2 seconds the cells gets selected. Is there a better location to put that code than viewWillAppear? – MichiZH Dec 13 '13 at 10:39
  • Change the delay to 0. It's not as clean as I wish but its the only solution I know that works. http://stackoverflow.com/questions/14020027/how-do-i-know-that-the-uicollectionview-has-been-loaded-completely – zeiteisen Dec 13 '13 at 10:50
  • I've put it into viewDidAppear with delay 0 and it works smooth :-) – MichiZH Dec 13 '13 at 10:51
  • NICE! For me I had to do this for *cell .... `ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCellIdentifier forIndexPath:indexPath];` – Albert Renshaw Jun 05 '14 at 04:37
0

After years selection seems to work fine (even in viewDidLoad:)

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_collection selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionLeft];

successfully triggers cell's -setSelected:

Varrry
  • 2,647
  • 1
  • 13
  • 27