Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select item/cell of UICollectionView from code

I have implemented a UICollectionView in my app. My problem is that I need to select (like if the user tapped on it) a cell programmatically. The method:

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                     animated:(BOOL)animated 
               scrollPosition:(UICollectionViewScrollPosition)scrollPosition

That is part of the UICollectionView class is not what i need to call, since this method does not call:

- (void)collectionView:(UICollectionView *)collectionView 
        didSelectItemAtIndexPath:(NSIndexPath *)indexPath

It just sets the selected property of the cell to YES;

like image 315
Nikola Kirev Avatar asked Dec 13 '12 12:12

Nikola Kirev


2 Answers

Yes, this is proper behaviour. Documentation for [selectItemAtIndexPath:animated:scrollPosition:] says:

This method does not cause any selection-related delegate methods to be called.

like image 69
leviathan Avatar answered Oct 20 '22 03:10

leviathan


I am calling [self collectionView:yourView didSelectItemAtIndexPath:yourIndexPath] to programmatically select a cell. But in the method cell is always nil. This works perfectly well when user selects a cell.

like image 25
Shirish Kumar Avatar answered Oct 20 '22 01:10

Shirish Kumar