У меня есть UITableViewController
вот так. Сгруппированный стол с изображением UIBarButtonItem
в аксессуаре каждой строки.Получить индексный путь от UIBarButtonItem в UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIToolbar *toolBar = [[TransparentToolbar alloc] init];
UIBarButtonItem *loginButton = [[UIBarButtonItem alloc] initWithTitle:@"Check"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(login:)];
toolBar.frame = CGRectMake(0, 0, 100, 103);
toolBar.items = @[loginButton];
cell.accessoryView = toolBar;
return cell;
}
Теперь я пытаюсь получить indexpath в UITableViewCell
при нажатии на соответствующую UIBarButtonItem
. (source)
- (void)login:(UIBarButtonItem *)button
{
UIBarButtonItem *barButton = button;
UIView *view = [barButton valueForKey:@"view"];
UITableViewCell *cell = (UITableViewCell *)view.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"secton: %d", indexPath.section);
}
Но это только возвращает 0 независимо от того, какой кнопки бара я нажимаю. Кто-нибудь может сказать мне, что с ним не так, и как исправить это? Я бы очень признателен.
спасибо.
Вы можете добавить «тег» в barButton в «tableView cellForRowAtIndexPath», а затем получить это значение «tag» в методе 'login'. –
Отъезд мой ответ –