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.
views:
147answers:
2Before 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).
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.