UICollectionView selectItemAtIndexPath不调用didSelectItemAtIndexPath

时间:2013-10-14 18:22:49

标签: objective-c uicollectionview

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

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

由于某种原因,功能:

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

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

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

这是一种正确的行为吗?

4 个答案:

答案 0 :(得分:35)

你可以做到。

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

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

快乐代码:)

答案 1 :(得分:23)

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

  

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

答案 2 :(得分:3)

Osmenda在Swift中使用我的自定义集合视图回答:

self.collectionView(calendarCV, didSelectItemAtIndexPath: indexPath)

答案 3 :(得分:1)

Swift 4:

self.collectionView(daysCollectionView, didSelectItemAt: indexPath)