xcode 如何从单元格中的按钮操作获取表格视图中的 indexPath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7452389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:57:21  来源:igfitidea点击:

How to get indexPath in a tableview from a button action in a cell?

xcodeuitableviewnsindexpath

提问by Sylvain Bessot

I have a tableview with two actions in it, for the first on i use the delegate didSelectRowAtIndexPath, for the second one i would like to use an action on a button on my cell to do something else. My problem is that i don't succeed to get the indexpath ? What is the best practice to do that.

我有一个包含两个操作的 tableview,第一个我使用委托 didSelectRowAtIndexPath,第二个我想在我的单元格上的按钮上使用一个操作来做其他事情。我的问题是我没有成功获得索引路径?这样做的最佳做法是什么。

回答by EmptyStack

If you have added the button to the cell as,

如果您已将按钮添加到单元格中,

[cell.contentView addSubview:button];

then, you can get the index path as,

然后,您可以获得索引路径,

- (void)onButtonTapped:(UIButton *)button {

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    // Go ahead
}

回答by Yogesh Kumar

I'm using this solution -- getting index path of cell using points

我正在使用这个解决方案——使用点获取单元格的索引路径

CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);

Its work perfectly

它的工作完美

回答by user2014551

here is the full code for a custom button:

这是自定义按钮的完整代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}

    //configure your cell here
    cell.titleLabel.text = @"some title";

    //add button
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    resetButton.frame = CGRectMake(40, 10, 18, 18);
    [resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal];
    [cell.contentView addSubview:myButton];

    //afterwards return the cell
    return cell;

- (void)onButtonTapped:(UIButton *)button {

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [filterTable indexPathForCell:cell];
    NSLog(@"%@", indexPath);
    //use indexPath here...
}

回答by Mario Jaramillo

In case you are using a collection view, you can use Yogesh method, but change indexPathForRowAtPoint for indexPathForItemAtPoint like this:

如果您使用的是集合视图,则可以使用 Yogesh 方法,但将 indexPathForRowAtPoint 更改为 indexPathForItemAtPoint,如下所示:

CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);

回答by mahboudz

In case that your button is moved or Apple changes the view hierarchy for accessory views, this method goes up the chain to look for a UITableViewCell:

如果您的按钮被移动或 Apple 更改了附件视图的视图层次结构,此方法将向上查找 UITableViewCell:

- (void)tapAccessoryButton:(UIButton *)sender {
    UIView *parentView = sender.superview;

// the loop should take care of any changes in the view heirarchy, whether from
// changes we make or apple makes.
    while (![parentView.class isSubclassOfClass:UITableViewCell.class])
        parentView = parentView.superview;

    if ([parentView.class isSubclassOfClass:UITableViewCell.class]) {
        UITableViewCell *cell = (UITableViewCell *) parentView;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
    }
}