1
votes

In my application I created a custom table view cell having a button inside it. I need to do specific actions when each button tapped. At the moment I could add actions to buttons, so that when I tapped, the actions are invoking but I cant identify which button is responsible for that action. How can I do different operations based on user event on buttons ? I could do this in normal way by setting "tag" property and checking using

[sender tag]

but dont know in this case. Thanks in advance.

3
Is your problem that you don't know which button in a cell has been pressed, or that you don't know which cell the button was in?jrturton
@jrturton i dont know which button get tapped,and since button layed so that it fully covered table cell, I could not get touch on cellrakeshNS

3 Answers

0
votes

Example:

[button1 addTarget: target action: @selector(action1) forControlEvents: UITouchUpInside];
[button2 addTarget: target action: @selector(action2) forControlEvents: UITouchUpInside];
[button3 addTarget: target action: @selector(action3) forControlEvents: UITouchUpInside];
0
votes

Make a for loop and assign each button an incremental tag (based on row number possibly), as well as a single method in the view control that is the target (all buttons get the same selector).

In the called method, extract the tag number and perform the necessary action on it.

0
votes

You're probably adding buttons to cells in cellForRowAtIndexPath.

If your table has only one section, then it's easy. Simply modify cellForRowAtIndexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // .. create cell

    // .. add button

    myButton.tag = indexPath.row;
    [myButton addTarget: target action: @selector(buttonClicked:) forControlEvents: UITouchUpInside];

    return cell;
}

-(void) buttonClicked:(id)sender {

  NSLog (@"user clicked button in row %d", [(UIButton *)sender tag]);

  // ..do your stuff here  
}