开发者

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

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 pro开发者_Python百科blem is that i don't succeed to get the indexpath ? What is the best practice to do that.


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
}


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


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


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);


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜