如何从UICollectionViewCell访问UICollectionView中Cell的indexPath [英] how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView

查看:853
本文介绍了如何从UICollectionViewCell访问UICollectionView中Cell的indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在调用操作时为 UICollectionViewCell 设置动画。

i已完成 UICollectionViewCell Interface Builder 中, UICollectionView 也是。
现在我想在我的 actionBtnAddToCard 方法中获得正确的 indexPath



这就是我现在尝试的方式(ProduktViewCell.m中的方法):

   - (IBAction )actionAddToCart:(id)sender {
XLog(@);

//看到这一行$​​ b $ b NSIndexPath * indexPath = ?? **如何才能访问正确的indexPath ** ??
SortimentViewController * svc = [[SortimentViewController alloc] initWithNibName:@SortimentViewController_iPadbundle:[NSBundle mainBundle]];
[svc.collectionViewProdukte cellForItemAtIndexPath:indexPath];

[svc collectionView:svc.collectionViewProdukte didSelectItemAtIndexPath:indexPath];
}

SortimentViewController是继承UICollectionView的viewController。

如何访问正确的indexPath?



更新1:已编辑的帖子以便更好地理解。

解决方案

如果您知道视图层次结构很容易。

  UIButton * button =(UiButton *)发件人;如果按钮是这样的  - > UITableViewCell  - >按钮 



那么你可以得到这样的单元格

  UITableViewCell * cell =(UITableViewCell *)[按钮superview]; 

如果按钮是这样的 - > UITableViewCell - >内容视图 - >按钮

  UITableViewCell * cell =(UITableViewCell *)[[button superview] superview]; 

最后可以像这样提取索引路径

  NSIndexPath * indexPath = [self.table_View indexPathForCell:cell]; 


i want to animate the UICollectionViewCell when action is called.
i have done UICollectionViewCell in Interface Builder, the UICollectionView also. Now i want to get the correct indexPath at my actionBtnAddToCard method.

thats the way i try it now (method in ProduktViewCell.m):

- (IBAction)actionAddToCart:(id)sender {
    XLog(@"");

    // see this line
    NSIndexPath *indexPath = ??** how can i access the correct indexPath**??;
    SortimentViewController *svc = [[SortimentViewController alloc] initWithNibName:@"SortimentViewController_iPad" bundle:[NSBundle mainBundle]];
    [svc.collectionViewProdukte cellForItemAtIndexPath:indexPath];

    [svc collectionView:svc.collectionViewProdukte didSelectItemAtIndexPath:indexPath];
} 

SortimentViewController is the viewController which inherits the UICollectionView.
how to acces the correct indexPath?

UPDATE 1: edited post for better understanding.

解决方案

if you know the view hierarchy it is easy.

UIButton *button = (UiButton *) sender;

if the button is like this - > UITableViewCell - > button

then you can get cell like this

UITableViewCell *cell = (UITableViewCell *)[button superview];

if the button is like this - > UITableViewCell - > content view -> button

UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

and finally index path can be extracted like this

NSIndexPath *indexPath = [self.table_View indexPathForCell:cell];

这篇关于如何从UICollectionViewCell访问UICollectionView中Cell的indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆