У меня есть довольно большая проблема. Я пытаюсь создать любимую кнопку на каждом UITableViewCell
в a UITableView
. Это работает очень хорошее, и у меня в настоящее время есть действие и селектор, выполненный при нажатии.
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 15, 15);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessory;
И селектор:
- (void) didTapStar {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */];
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown];
newCell.accessoryView = accessory;
}
Теперь, вот проблема: Я хочу знать, какой строке аксессуар, который был нажат, принадлежит. Как я могу сделать это?
Спасибо :)
Я бы использовал следующий код:
- (void)didTapStar:(id)sender {
NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]];
}
Немного измените свое действие.
- (void) действие: (id) отправитель forEvent: (UIEvent *) event
Внутри этого метода:
NSIndexPath * indexPath = [tableView indexPathForRowAtPoint: [[[event touchesForView: sender] anyObject] locationInView: tableView]];
Это должно дать вам путь индекса строки.