简体   繁体   English

UICollectionView selectItemAtIndexPath 不调用 didSelectItemAtIndexPath

[英]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?这是一种正确的行为吗?

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.如果只使用 selectItemAtIndexPath:animated:scrollPosition: 方法,不要调用委托方法。 if use only collectionView:didSelectItemAtIndexPath: method, it don't work stabil如果只使用 collectionView:didSelectItemAtIndexPath: 方法,它不会稳定工作

happy code :)快乐代码:)

Yes, this is proper behaviour.是的,这是正确的行为。 Documentation for [selectItemAtIndexPath:animated:scrollPosition:] says: [selectItemAtIndexPath:animated:scrollPosition:] 的文档说:

This method does not cause any selection-related delegate methods to be called.此方法不会导致调用任何与选择相关的委托方法。

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

self.collectionView(calendarCV, didSelectItemAtIndexPath: indexPath)

斯威夫特 4:

self.collectionView(daysCollectionView, didSelectItemAt: indexPath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM