0

i have a UITable with UITableCells which look as the following:

[ myLabel myButton ]

[ myLabel myButton ]

[ myLabel myButton ]

[ myLabel myButton ]

I would like my handlerCellButtonClicked to also get a reference to the cell index. How can I get a reference to this index as well when the button is touched?

/** called by table to get the cell needed by the index */
-(UITableViewCell*)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    //init cell
    ...
    //configure the cells custom button handlers
    [cell.myButton addTarget:self action:@selector(handlerCellButtonClicked) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

/** handler when the button in the cell is clicked */
- (void)handlerCellButtonClicked{
    NSLog(@"cell myButton clicked");
}
mihai
  • 4,184
  • 3
  • 26
  • 27
  • possible duplicate of [Detecting which UIButton was pressed in a UITableView](http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) – Matthias Bauch Jan 17 '14 at 21:39

1 Answers1

2

Make use of UIButton Tag property.

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

    //init cell
    //configure the cells custom button handlers

    cell.myButton.tag = indexPath.row;
    [cell.myButton addTarget:self action:@selector(handlerCellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

/** handler when the button in the cell is clicked */
- (void)handlerCellButtonClicked:(UIButton *)sender
  {
     NSLog(@"%d cell myButton is clicked", sender.tag);
  }
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • How are you sure that selector will pass the sender? – hgwhittle Jan 17 '14 at 21:06
  • (handlerCellButtonClicked:) should pass the sender to the target, right? – Shamsudheen TK Jan 17 '14 at 21:08
  • You can only Pass a (UIButton*) object as a parameter with addTarget @selector method. thats it. Also the "UIControlEventTouchUpInside" event will send the button as parameter.hope you understood. – Shamsudheen TK Jan 17 '14 at 21:11
  • @Ramshad: this worked great, adding the colon @selector(handlerCellButtonClicked:) after the method sends the UIButton and i can get the tag. The problem I have is that if I delete rows the tag is incorrect since the indexs have changed. But I can post this as another question. Thank you for your help. – mihai Jan 17 '14 at 21:18
  • @Ramshad does the tag have to be NSInteger or can it be any object? – mihai Jan 17 '14 at 21:22
  • @mihai: tag have to be NSInteger however you can pass other datas.this is the answer for your quesion http://stackoverflow.com/questions/510393/how-to-pass-a-variable-to-a-uibutton-action – Shamsudheen TK Jan 17 '14 at 21:27
  • I think, a tableview reload after a delete will fix your issue. use [myTableView reloadData]; to do this. :) – or create your own logic some think like, if you are deleted the 4th row, then if the button tag is 5, then minus 1 value from it,,,. – Shamsudheen TK Jan 17 '14 at 21:29
  • @Ramshad thank you very much! I was able to fix my issue. Originally i was using the index of the cell to reference the index of the array. Thats a bad pattern when tag can be anything. So I made tag be a key in a dictionary and now it doesnt matter the order. thanks again you were a great help. – mihai Jan 17 '14 at 22:35