Get the indexpath from a UIBarButtonItem in a UITableViewCell

I have a UITableViewController like this. A grouped table view with a UIBarButtonItem in the accessoryView of each row.

- (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"
    toolBar.frame = CGRectMake(0, 0, 100, 103);
    toolBar.items = @[loginButton];
    cell.accessoryView = toolBar;

    return cell;

Now I'm trying to get the indexpath of the UITableViewCell when clicked upon the corresponding 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);

But it only returns 0 no matter which bar button I click. Anyone can tell me what's wrong with it and how to correct this? I'd really appreciate it.

Thank you.


Try using view.superview.superview; instead of view.superview;

Need Your Help

How to combine multiple php/mysql database connections?

php oop

When building dynamic websites I use a php include to make my connections to the database. The included file is very basic:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.