-2

I created a dynamic prototype cell in storyboard and this cell is containing a UILabel and two UIButtons.

Number of rows depends on my data which I am fetching from web service.

I Don't want to subclass this cell.

I am fetching controls reference inside the cell using this code [cell viewWithTag:0].

And for tap event I am using this code

[cell.button addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];

Problem is how to detect which row's button is tapped?

S.J
  • 3,063
  • 3
  • 33
  • 66

2 Answers2

0

Write this inside cellforRowAtIndexPath

UIButton *button = (UIButton *)[cell viewWithTag:100]
[button setTag:indexPath.row];

and in your action method write

-(void)myMethod:(UIButton *)sender
{
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
      // indexPath contains clicked row indexpath

}
codeIgnitor
  • 765
  • 1
  • 5
  • 16
  • Read my question again. – S.J Aug 19 '14 at 09:06
  • @S.J Please Explain me, You have not subclassed your Table view cell hence u cant create outlet connection for your buttons(cos Connection cannot have prototype object as its destination) but how you are referring like `cell.button`????? – codeIgnitor Aug 19 '14 at 09:49
-1

in cellForRow method you add the tag to the button just like this:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"identif"];
       cell.button.tag = indexPath.row; // the tag of the button will be the index of the cell; 

}

it is recommended to use some kind of formula for the tag something like: 100+indexPath.row, because every View's tag in your application is initially 0.

after that, in your button selector just do this:

- (void)myMethod:(id)sender {
   UIButton *button = (UIButton *)sender;
 NSLog(@"%ld",(long)buttton.tag); 
}
Bogdan Somlea
  • 604
  • 3
  • 15
  • please view my question again. Thank you. – S.J Aug 19 '14 at 08:55
  • and what i said is correct. In the method just get the tag. check my edit – Bogdan Somlea Aug 19 '14 at 10:18
  • No you are wrong because I said I don't want to subclass. – S.J Aug 19 '14 at 11:55
  • you can't do it from storyboard if you don't subclass UITableViewCell, where do you add the action and the outlet? in what class? – Bogdan Somlea Aug 19 '14 at 11:57
  • I have also mentioned this in my question, that is why I am saying read my question properly. – S.J Aug 19 '14 at 12:27
  • you are adding prototypeCells, and in the prototypeCells you add the buttons. You can't add the outlet of the buttons if you don't subclass UITableViewCell. the way you want it, it's not possible – Bogdan Somlea Aug 19 '14 at 12:41
  • You are not reading my question, everything is mention there and I have also solved this issue, visit http://stackoverflow.com/questions/3910823/uitableviewcell-with-a-uibutton – S.J Aug 19 '14 at 12:54