views:

79

answers:

1

I have a table view where each cell has a button accessory view. The table is managed by a fatchedresults controller and is frequently reordered. I want to be able to press a button and be able to obtain the index path of the pressed button's tableviewcell. I've been trying to get this working for days by storing the row of the button in it's tag, but when the table get's reordered the row becomes incorrect and I keep failing at reordering the tags correctly when a change is made. Any new ideas on how to keep track of the button's cell's index path? Thanks so much for any help.

A: 
- (IBAction)clickedButton:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}

Or shorter:

- (IBAction)clickedButton:(id)sender {
    NSIndexPath *indexPath = [(UITableView *)sender.superview.superview indexPathForCell:(UITableViewCell *)sender.superview];
}

Both are untested!

Douwe Maan
Thanks so much! This fixed the issue and now I can finally move on :)For those who get here later and want to implement this, in the first example you need one less superview call to get the UITableViewCell and superview is all lowercase. Otherwise, everything is perfect (I haven't tried the shorter example).
Jake
You're right, edited my answer ;) My logic behind the 2 `.superviews` to get the `UITableViewCell`: the cell is the `superview` of the `accessoryView` which is the `superview` of the `UIButton`. But I guess I was mistaken.
Douwe Maan