Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Collection view, selectItemAtIndexPath programmatically doesn't work

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];
}
like image 310
MichiZH Avatar asked Dec 25 '22 16:12

MichiZH


1 Answers

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.

like image 82
zeiteisen Avatar answered Jan 31 '23 06:01

zeiteisen