如何从 UICollectionViewCell 访问 UICollectionView 中 Cell 的 indexPath [英] how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView
问题描述
我想在调用操作时为 UICollectionViewCell
设置动画.
我已经在 Interface Builder
中完成了 UICollectionViewCell
,UICollectionView
也是如此.现在我想在我的 actionBtnAddToCard
方法中获得正确的 indexPath
.
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.
这就是我现在尝试的方式(ProduktViewCell.m 中的方法):
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 是继承 UICollectionView 的 viewController.
如何访问正确的 indexPath?
SortimentViewController is the viewController which inherits the UICollectionView.
how to acces the correct indexPath?
更新 1:编辑帖子以便更好地理解.
推荐答案
如果你知道视图层次结构就很容易了.
if you know the view hierarchy it is easy.
UIButton *button = (UiButton *) sender;
如果按钮是这样的-> UITableViewCell->按钮
那么你可以得到这样的单元格
then you can get cell like this
UITableViewCell *cell = (UITableViewCell *)[button superview];
如果按钮是这样的-> UITableViewCell -> 内容视图-> 按钮
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屋!