如何从UICollectionViewCell访问UICollectionView中Cell的indexPath

bru*_*h51 20 nsindexpath ibaction ios uicollectionview uicollectionviewcell

我想动画UICollectionViewCell调用何时动作.
我已经做UICollectionViewCellInterface Builder,该UICollectionView还.现在我想indexPath在我的actionBtnAddToCard方法中得到正确的答案.

这就是我现在尝试的方式(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];
} 
Run Code Online (Sandbox Code Playgroud)

SortimentViewController是viewController,它继承了UICollectionView.
如何访问正确的indexPath?

更新1:编辑帖子以便更好地理解.

car*_*ich 25

- (IBAction)actionAddToCart:(id)sender {
   NSIndexPath *indexPath;
   indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]]; 
   ...
}
Run Code Online (Sandbox Code Playgroud)


Shu*_*ank 17

如果你知道视图层次结构很容易.

UIButton *button = (UiButton *) sender;
Run Code Online (Sandbox Code Playgroud)

如果按钮是这样的 - > UITableViewCell - >按钮

然后你可以像这样得到细胞

UITableViewCell *cell = (UITableViewCell *)[button superview];
Run Code Online (Sandbox Code Playgroud)

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

UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
Run Code Online (Sandbox Code Playgroud)

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

NSIndexPath *indexPath = [self.table_View indexPathForCell:cell];
Run Code Online (Sandbox Code Playgroud)

  • OP已经要求UICollectionView不是UITableViewCell的情况下的解决方案..恕我直言这个答案向读者发送错误信息..请编辑以指出如何在UICollectionView中执行此操作. (28认同)
  • 使用UICollectionViewCell对我不起作用 (18认同)

Ego*_*yer 7

不依赖于视图.试试这个.

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:buttonPosition];

NSLog(@"%ld", (long)indexPath.row);
Run Code Online (Sandbox Code Playgroud)


Ech*_*lon 6

使用像[[button superview] superview]脆弱的代码,而不是面向未来; 实际上,除非您明确测试它,否则它甚至不能保证在所有iOS版本上运行.为此,我总是使用迭代辅助方法: -

- (UIView *)superviewWithClassName:(NSString *)className fromView:(UIView *)view
{
    while (view)
    {
        if ([NSStringFromClass([view class]) isEqualToString:className])
        {
            return view;
        }
        view = view.superview;
    }
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

然后我从按钮处理程序中调用它,如下所示: -

- (IBAction)buttonClicked:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UICollectionViewCell *cell = (UICollectionViewCell *)
                                [self superviewWithClassName:@"UICollectionViewCell"
                                fromView:button];
    if (cell)
    {
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
        // do whatever action you need with the indexPath...
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:Swift版本superviewWithClassName.使它成为一种类方法,因为它从不引用self.

static func superviewWithClassName(className:String, fromView view:UIView?) -> UIView? {
    guard let classType = NSClassFromString(className) else {
        return nil
    }
    var v:UIView? = view
    while (v != nil) {
        if v!.isKindOfClass(classType) {
            return v
        }
        v = v!.superview
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)

和一些代码来调用它,或从prepareForSegue一个按钮处理程序: -

guard let cell = UIView.superviewWithClassName("UICollectionViewCell", fromView: sender as? UIView) as? UITableViewCell else {return}
Run Code Online (Sandbox Code Playgroud)


Ish*_*nda 5

Swift解决方案:像这样的UICollectionView扩展可能对此有用.

extension UICollectionView {
    func indexPathForView(view: AnyObject) -> NSIndexPath? {
        let originInCollectioView = self.convertPoint(CGPointZero, fromView: (view as! UIView))
        return self.indexPathForItemAtPoint(originInCollectioView)
    }
}
Run Code Online (Sandbox Code Playgroud)

到处使用变得容易.

let indexPath = collectionView.indexPathForView(button)
Run Code Online (Sandbox Code Playgroud)