0

I have a prototype UITableViewCell with 3 buttons, which I'll call button1, button2 and button3.

Say for example I load an array of 2 items into my UITableView, so 2 instances of this prototype UITableViewCell, each with 3 buttons; how do I handle actions from these buttons and determine which button was tapped on which row.

Here is a screenshot of the prototype cell I am using.

UPDATE

Using the code below, the row is always 0...Any ideas?

ParentDashboardChildTableViewCell *cell = (ParentDashboardChildTableViewCell *)[sender superview].superview;
    NSIndexPath *indexPath = [self.overviewTableView indexPathForCell:cell];
    NSLog(@"selected tableview row is %ld", (long)indexPath.row);
nickjf89
  • 458
  • 1
  • 7
  • 18

2 Answers2

1

You can use tag for each of those buttons. For reference

Assign tag values for the three buttons on the table cell. You can assign the tag values in a format like: 00,01,02 so that o refers to row number and 1,2,3 refers to the buttons.

      NSString *tagString_for_Button1=[NSString stringwithformat:@"%d%d"indexPath.row,1];
         NSString *tagString_for_Button2=[NSString stringwithformat:@"%d%d"indexPath.row,2];
            NSString *tagString_for_Button3=[NSString stringwithformat:@"%d%d"indexPath.row,3];
        cell.yourbutton1.tag = [tagString_for_Button1 integerValue];
         cell.yourbutton2.tag = [tagString_for_Button2 integerValue];                    
           cell.yourbutton3.tag = [tagString_for_Button3 integerValue];

and add target action to your buttons as below:

    [cell.yourbutton1 addTarget:self action:@selector(yourButtonClicked1:) forControlEvents:UIControlEventTouchUpInside];
    [cell.yourbutton2 addTarget:self action:@selector(yourButtonClicked2:) forControlEvents:UIControlEventTouchUpInside];
    [cell.yourbutton3 addTarget:self action:@selector(yourButtonClicked3:) forControlEvents:UIControlEventTouchUpInside];

 and define the methods for those buttons based on index as follows:
     -(void)yourButtonClicked:(UIButton*)sender
   {
 if (sender.tag == 0) 
 {
 //Here your coding.

 }
 }
Community
  • 1
  • 1
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

Suppose you have defined the target action of you button to a method called "actionCellButtonClicked" then you can implement the method like that

-(IBAction)actionCellButtonClicked:(id)sender {
    CustomCollectionCell *cell = (CustomCollectionCell *) [sender superview];
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    // to do somthing
 }

Note: [sender superview] it allows to traverse the view hierarchy that contains you buttons. In other words you get the cell and than you can get the index of the cell. I used collectionView but there is an equivalent method for table view.

chiarotto.alessandro
  • 1,491
  • 1
  • 13
  • 31
  • Thanks for the answer. I tried to implement this using the updated code in my question, but the row is always 0 apparently...Any ideas? – nickjf89 Mar 03 '15 at 16:52
  • If the value of NSIndexPath.row is always 0, try to log the cell in order to check if you get different cell. If you get different cell (different id) then try to implement this method of uitable: tableView:heightForRowAtIndexPath and check that your custom cell respect the value returned by the above method. – chiarotto.alessandro Mar 03 '15 at 21:52