views:

147

answers:

2

I've added a button as an accessory view to uitableviewcell, and I when I press it, I'd like to be able to access the index path of the table view cell it's currently inside of so I can delete/modify the contents of that cell. Should I subclass UIbutton and add make it have it's own index path property? If so, do I need to implement any specific button methods in that subclass of will they automatically be loaded? Any help would be greatly appreciated. Sorry if this is a noobie question.

A: 

Before returning the UIButton as the accessoryView, set the UIButton's tag to the indexPath of the current cell.

Then when you tap the Button and it's selector is called, you can determine the indexPath.row from the tag (sender's tag).

Jordan
Thanks for the response. Isn't tag supposed to be an integer, not a pointer though? What you said seems to work, but I'm getting a warning saying that I'm making an integer from a pointer.
Jake
You should be able to put indexPath.row into .tag without a problem. Maybe post some code and we'll take a look.
Jordan
A: 

Your solution of subclassing is good, but only works with custom buttons. [UIButton buttonWithType:] is allowed to return private subclasses, which you are not supposed to subclass. You would not need to implement any methods beyond synthesizing the indexPath property.

An inelegant but functional solution would be to walk up superviews from the button until the view is a UITableViewCell, then ask the table view for the index path of that cell.

If you are not using sections, you could slip the row of the cell into the button tag. Not robust or elegant, but it works.

You could make a custom subclass of UITableViewCell and use it as the action target for the button. It would then call through to your current action target passing itself as the sender.

drawnonward
Thanks, I think I'm going to end up doing a combination of paragraph 1 and 3 since I need to implement a few other custom things.
Jake