0

I have UIButtons on each row of a UITableView. How can I detect ,the button index (like we can detect the row index in table through didSelectRowIndexAtIndexPath method)?

Say for ex: I have a UITableView with custom UITableViewCells. Out of 10 rows, 6 have UIButtons.

On button tap, I am taking user to next view(detail view). But I am not able to get the UIButton (row) index.Since, I am taking user on button click not through didSelectRowIndexAtIndexPath method, I am not able to get the row index.

How can i get that?

Suggestion. Thanks

geminiCoder
  • 2,918
  • 2
  • 29
  • 50
FirstTimer
  • 313
  • 1
  • 5
  • 14

1 Answers1

3

Do this in:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //other code
  // your button reference here
  // if custom cell then use cell.yourbutton
  [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
  [button setTag:indexPath.row];
  //other code
}

Now buttons selector would belike this:

- (void)buttonPressedAction:(id)sender
{
   UIButton *button = (UIButton *)sender;
   int row = button.tag;
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132