UICollectionView selectItemAtIndexPath不调用didSelectItemAtIndexPath

bas*_*han 13 objective-c uicollectionview

我有一个集合视图.我想以编程方式选择一个单元格.这是我使用的代码

[_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:currentSelectedVideo inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)

由于某种原因,功能:

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

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

未调用,因此我用于显示所选单元格的视觉效果未显示在屏幕上.

这是一种正确的行为吗?

小智 36

你可以做到.

[self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

如果只使用selectItemAtIndexPath:animated:scrollPosition:方法,请不要调用委托方法.如果只使用collectionView:didSelectItemAtIndexPath:方法,它不起作用稳定

快乐的代码:)


小智 23

是的,这是正确的行为.[selectItemAtIndexPath:animated:scrollPosition:]的文档说:

此方法不会导致调用任何与选择相关的委托方法.