6

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;

AndyG
  • 39,700
  • 8
  • 109
  • 143
Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30
  • 2
    What about calling the method yourself after you selected your cell? You could call [self collectionView:yourView didSelectItemAtIndexPath:yourIndexPath]; – Boris Prohaska Dec 13 '12 at 12:55
  • 1
    Maybe this answer will help you http://stackoverflow.com/questions/13177201/select-items-programmatically-in-uicollectionview – Диляна Тодорова Mar 01 '13 at 11:09
  • @ДилянаТодорова Yes. Thanks! I knew about the `didSelectItemAtIndexPath` one, but I was looking for a method that is not part od the `UICollectionView Delegate Protocol`. I figured it out. It all works fine now. – Nikola Kirev Mar 01 '13 at 15:37

3 Answers3

9

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

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

leviathan
  • 11,080
  • 5
  • 42
  • 40
8

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.

Shirish Kumar
  • 1,532
  • 17
  • 23
  • 8
    or in swift you could use: `collectionView.delegate?.collectionView!(collectionView, didSelectItemAtIndexPath: someIndexPath)` – mbuff24 May 04 '15 at 20:58
  • That works, it's a bad practice though to call delegate methods directly (the Collection View should invoke that method on its delegate as Apple recommends). As @leviathan mentioned "[selectItemAtIndexPath:animated:scrollPosition:] " will not invoke the delegate method so there are better ways to work around that. – ppalancica Mar 20 '17 at 21:01
7

First, you need to make the target cell visible, otherwise [yourCollectionView cellForItemAtIndexPath:yourIndexPath] always returns nil.

// scroll to the cell
[yourCollectionView scrollToItemAtIndexPath:yourIndexPath
                           atScrollPosition:UICollectionViewScrollPositionBottom 
                                   animated:NO];

// call delegate method
[self collectionView:yourCollectionView didSelectItemAtIndexPath:yourIndexPath];

// now you have selected the cell and can do anything to it in the delegate method :-) 
nopopon
  • 257
  • 3
  • 6
  • My cell is always visible, but the cellForItemAtIndexPath always return nil (even after call scrollToItemAtIndexPath – Bagusflyer Jul 19 '14 at 09:36