Custom cells? Fine. Look:
1. create a UIButton subclass (IndexedButton for example), add a property to it
@property (nonatomic) NSIndexPath *button_indexPath;
In IndexedButton.m, do @synthesize button_indexPath;
2. In the class of your custom cell in the .h file you connect this IndexedButton:
#import "IndexedButton.h"
and the button to be pressed is not done by UIButton , but by IndexedButton :
@property (nonatomic, retain) IBOutlet IndexedButton *buttonInCell;
In the custom cell m-file, make @synthesize buttonInCell;
There you add this button to the cell in the method:
-(id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self){ buttonInCell = [[IndexedButton alloc] initWithFrame:CGRectMake(0,0,50,30)]; [self addSubview:buttonInCell]; // здесь еще твой код } return self; }
3. Almost all. Now in the main class, the cellForRowAtIndexPath method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath { // тут подключение твоей кастомной ячейки if (cell == nil) { //Создание ячейки cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.buttonInCell.button_indexPath = indexPath; //присваиваем кнопке значение indexPath [cell.buttonInCell addTarget:self action:@selector(MyMethod:) forControlEvents:UIControlEventTouchUpInside]; //устанавливаем обработчик для нажатия кнопки // здесь еще твой код return cell; }
4. Finish- MyMethod method:
-(IBAction)MyMethod:(id)sender{ NSIndexPath *path = [sender button_indexPath]; //извлекаем IndexPath с кнопки. //наслаждаемся результатом NSLog(@"секция = %i", path.section); NSLog(@"ряд = %i", path.row); }
Hope clarified available.