There are many ways to get the indexPath
of cell from the button was clicked:
1. Protocol
// In your customCell.h file have a Delegate method
@protocol customCellDelegate <NSObject>
- (void)btn_ClickedForCell:(id)cell;
@end
@interface customCell : UITableViewCell
@property (nonatomic,assign)id<ChatOptionsPopUpDelegate>delegate;
// In your customCell.m
File call this delegate method on click of button
- (void)btnAction{
if ([_delegate respondsToSelector:@selector(btn_ClickedForCell:)]) {
[_delegate btn_ClickedForCell:self];
}
}
// Now in you mainVC.m
File assign this delegate
@interface mainVC () <customCellDelegate>
// in your `cellForRowAtIndexPath`
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
cell.delegate = self;
...
}
// So whenever your button gets clicked the function will be called
- (void)btn_ClickedForCell:(id)cell{
NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
// Enjoy you got the indexPath
}
- By Tag
// give tag to you button and get its cell by tag
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:@selector(btn_Clicked:) forControlEvents:UIControlEventTouchUpInside];
...
}
- (void)btn_Clicked:(UIButton*)sender{
int tag = sender.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tag inSection:0];
// Enjoy you got the tag
}