How to get indexPath in a tableview from a button

2019-04-07 17:19发布

问题:

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.

回答1:

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
}


回答2:

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



回答3:

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...
}


回答4:

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

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


回答5:

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:

- (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];
    }
}