bashan
bashan

Reputation: 3602

UICollectionView selectItemAtIndexPath doesn't call didSelectItemAtIndexPath

I have a collection view. I would like to programmatically select a cell. This is the code I use

[_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:currentSelectedVideo inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];

For some reason the functions:

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

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

Are not called and therefore the visual effect I am using for showing the selected cell is not shown on the screen.

Is it a proper behavior?

Upvotes: 15

Views: 14793

Answers (4)

えるまる
えるまる

Reputation: 2551

Swift 4:

self.collectionView(daysCollectionView, didSelectItemAt: indexPath)

Upvotes: 2

Michael McKenna
Michael McKenna

Reputation: 794

Osmenda's answer in Swift with my custom collection view:

self.collectionView(calendarCV, didSelectItemAtIndexPath: indexPath)

Upvotes: 3

osmenda
osmenda

Reputation: 391

You can make it.

[self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];

if use only selectItemAtIndexPath:animated:scrollPosition: method, don't call delegate method. if use only collectionView:didSelectItemAtIndexPath: method, it don't work stabil

happy code :)

Upvotes: 39

Igor
Igor

Reputation: 1332

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

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

Upvotes: 26

Related Questions