0

I have an UITableView. There are 5 rows and 4 buttons each in a row. When the user taps the button i want to retrieve the row ID.

I have used UITapGestureRecognizer to recognise the Button that was selected. Now i want to find the Row the button is.

I wanted to use cell.myButton.tag =indexRow.row;. However, i am not able to receive it from ImageTapped method. Can someone help me out ?

Following code is part of cellForRowAtIndexPath cellForRowAtIndexPath

UITapGestureRecognizer *tap = nil;

            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageTapped:)];

            [cell.myButton addGestureRecognizer:tap];

... Like wise for the other button as well.

ImageTapped method

- (void) ImageTapped:(UITapGestureRecognizer *) recognizer {
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {

    }
}
Hima
  • 1,249
  • 1
  • 14
  • 18
Illep
  • 16,375
  • 46
  • 171
  • 302
  • @iphonic : **TapGesture don't work with UIButtons**, who told you that? – Fahim Parkar Mar 23 '15 at 05:58
  • @iphonic : ***I tried & used TapGesture for finding double taps on UIButton.*** [example](http://stackoverflow.com/questions/13750014/ios-double-tap-on-uibutton) – Fahim Parkar Mar 23 '15 at 06:25
  • @FahimParkar Applogies Sir, it does work, I was in different thoughts, thanks for clarification.. – iphonic Mar 23 '15 at 06:39

2 Answers2

0

First of all why are you using gesture and there no need to add gesture. You can do this thing by using addTarget Method.

In this method cellForRowAtIndexPath:

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

and you can get in this method:

- (void) ImageTapped:(id) sender {
    int rowId = [(UIButton *)sender tag];
}

Other wise you can get like this:

cell.myButton.tag = indexPath.row;// In cellForRowAtIndexPath Method

- (void) ImageTapped:(UITapGestureRecognizer *) recognizer {

    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        int rowId = recognizer.view.tag;  
    }

}
Bista
  • 7,869
  • 3
  • 27
  • 55
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

the button will be cell's descendent child. so you can get cell with the follow code:

id view;
while (![(view = button.superView)  isKindOfClass:[UITableViewCell class]]) ;
// now view will be the parent cell.
SolaWing
  • 1,652
  • 12
  • 14
  • ... and then you use indexPathOfCell to get the index path of the cell. Which is much better than getting a tag, because it will give you the indexPath even if rows have been rearranged, so this is a solution that scales. – gnasher729 Feb 18 '16 at 07:13