2013-08-06 2 views
1

У меня есть UITableViewController вот так. Сгруппированный стол с изображением UIBarButtonItem в аксессуаре каждой строки.Получить индексный путь от UIBarButtonItem в UITableViewCell

enter image description here

- (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 независимо от того, какой кнопки бара я нажимаю. Кто-нибудь может сказать мне, что с ним не так, и как исправить это? Я бы очень признателен.

спасибо.

+0

Вы можете добавить «тег» в barButton в «tableView cellForRowAtIndexPath», а затем получить это значение «tag» в методе 'login'. –

+0

Отъезд мой ответ –

ответ

3

Попробуйте использовать view.superview.superview; вместо view.superview;

+0

Ничего себе! это сработало! Это гораздо более элегантный способ. Не могли бы вы объяснить, как эта строка - 'view.superview.superview;' работает в моем случае? Спасибо за ответ кстати. :) – Isuru

+0

Я не могу проверить это в данный момент, но я думаю, что view.superview является accesoryView ячейки в этом случае. Ячейка контролирует его. – Kirsteins

+0

А я вижу. Еще раз спасибо. – Isuru

-1

установить другой тег для каждого элемента barbutton в каждом cell.then по кнопке теге вы можете найти, в какой ячейке это is.you доступ, как это

UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath]; 
1

В cellForRowAtIndexPath:

[loginButton setTag:indexPath.section]; 

Логин:

NSLog(@"secton: %d",(long int) [barButton tag]); 
2

установить loginutton тег, как таким образом

loginButton.tag=indexPath.section; 

прибудете indexpath в случае щелчка

- (void)login:(UIBarButtonItem *)button 
{ 
UITableViewCell *cell= [tableView cellForRowAtIndexPath:button.tag]; 
} 
1

CellForRow: -

loginButton.tag=indexPath.row; 

Войти Действие Метод: -

NSIndexPath *indexPath=[NSIndexPath indexPathForItem:button.tag inSection:0]; 
+0

@read вопрос тщательно .. indexPath.row прав? –

Смежные вопросы