UICollectionView selectItemAtIndexPath doesn't call didSelectItemAtIndexPath

不问归期 提交于 2020-01-01 02:06:09

问题


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?


回答1:


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

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




回答2:


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 :)




回答3:


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

self.collectionView(calendarCV, didSelectItemAtIndexPath: indexPath)



回答4:


Swift 4:

self.collectionView(daysCollectionView, didSelectItemAt: indexPath)


来源:https://stackoverflow.com/questions/19366637/uicollectionview-selectitematindexpath-doesnt-call-didselectitematindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!