0

I have a custom TableView cell, and in that there's a button. The table view have many rows used the same custom cell. Now if the button of one of the cells pressed. I want to know which row of cell the button is in?

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
leizh007
  • 71
  • 1
  • 7
  • You can set the button's tag property to the row index. – 0x141E Oct 11 '14 at 07:45
  • http://stackoverflow.com/questions/11936126/how-to-pass-uitableview-indexpath-to-uibutton-selector-by-parameters-in-ios/11936294#11936294 – TheTiger Dec 17 '14 at 11:43

2 Answers2

3

(1). In this method cellForRowAtIndexPath: , assign button with a tag. For example:

cell.yourbutton.tag = indexPath.row;

(2). Add action for you button with same selector

[cell.yourbutton addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

(3). Then

-(void)cellButtonClicked:(UIButton*)sender
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag inSection:0];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Leo
  • 24,596
  • 11
  • 71
  • 92
-2

You can define your custom property and you can use it like below:-

#define kCustomProperty @"CustomProperty" 

Associate your object with that custom property like below

objc_setAssociatedObject(yourButton,kCustomProperty , yourCellIndexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

Get your data using the same property and object like below

NSIndexPath *yourCellIndexPath = (NSIndexPath *)objc_getAssociatedObject(yourButton, kCustomProperty);

Its a kind of custom property you can create by coding if you don't want to use tag.

Leena
  • 2,678
  • 1
  • 30
  • 43