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?
Asked
Active
Viewed 289 times
0
-
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 Answers
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
-
the questioner need the row number not the button index , just modify your answer – Anbu.Karthik Oct 11 '14 at 07:47
-
if i make a custome cell and in code i set the button tag, are the button tag in different rows of cell have the same button tag or different? – leizh007 Oct 11 '14 at 08:28
-
-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