views:

250

answers:

2

Hi, I am a newbie.

I have a custom UITableViewCell with 3 items:

a UILabel a UITextField and a UIButton

My question is, how do I know from which cell the button was clicked or textField was edited ?

Is there a way to track them?

Thanks for any replies.

+1  A: 

What I usually do is after configuring the custom cell, I save the NSIndexPath of the cell in the title of the button for the state UIControlStateApplication.

In your tableView: cellForRowAtIndexPath: method, when configuring the cell:

[theButton setTitle:(NSString *)indexPath forState:UIControlStateApplication];

Then, in your button action:


- (IBAction) buttonWasClicked:(UIButton *) senderButton {
    NSIndexPath *indexPath;

    indexPath = (NSIndexPath *)[senderButton titleForState:UIControlStateApplication];

    NSLog(@"Sender button was clicked in cell at NSIndexPath section: %d row: %d ", [indexPath section], [indexPath row]);
}

I am not really happy with this way. If someone has a cleaner way to do it, I would love to hear about it.

Guillaume
+1  A: 

If you only have one section you can store the indexPath.row in the button.tag and retrieve it in the action that is triggered by that button.

Felix