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?
(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];
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.