简体   繁体   English

获取UICollectionViewCell的indexPath

[英]Get indexPath of UICollectionViewCell

I have a custom CollectionViewCell class, I have then generated an amount of these inside of a CollectionView. 我有一个自定义的CollectionViewCell类,然后在CollectionView中生成了许多此类。 I have then also added a modal segue when the ViewCells are touched. 然后,当触摸ViewCell时,我还添加了一个模式选择。 How would I get the id (or index path) of which cell was pressed when the modal segue is activated. 激活模式搜索后,如何获取按下哪个单元格的ID(或索引路径)。

I saw on here that someone suggested I add into the ViewCell.m file the method 我在这里看到有人建议我将方法添加到ViewCell.m文件中

-(void)collectionView: (UICollectionView*)collectionView didSelectItemAtIndexPath:   (NSIndexPath*)indexPath{
//get indexpath variable in here
}

But this method does not seem to be getting called. 但是似乎没有调用此方法。 Can anyone help me with what method would need to be get called for when a CollectionViewCell has a modal segue to get the indexpath. 任何人都可以帮助我,当CollectionViewCell具有模式搜索来获取索引路径时需要调用哪种方法。

Thanks, 谢谢,

If the views you have added to your cells are handling the touch by firing a segue, then the collectionViewCell never has a chance to receive the touch. 如果您添加到单元格中的视图通过触发segue处理触摸,则collectionViewCell永远没有机会接收触摸。

If you want the collectionViewCell to handle it, make the subviews of your cell purely decorative by disabling their behavior to fire the segue. 如果您希望collectionViewCell处理它,请通过禁用它们的行为来触发segue,使单元的子视图成为纯装饰性的。 (for example, if they are buttons and " touchUpInside " fires the segue, then change them into views with no touch handling at all.). (例如,如果它们是按钮,并且使用“ touchUpInside ”触发segue,则将其更改为完全无需触摸处理的视图。)

Then the touch will be passed up through the view hierarchy until the CollectionViewCell handles it, which it does with the collectionView:didSelectItemAtIndexPath: method. 然后,触摸将在视图层次结构中向上传递,直到CollectionViewCell处理为止,并使用collectionView:didSelectItemAtIndexPath:方法进行处理。

The method is a callback of the delegate class of the collection view object: 该方法是集合视图对象的委托类的回调:

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

So you should implement on the .m file of the delegate class of you collection view. 因此,您应该在集合视图的委托类的.m文件上实现。

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

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