1

我有一个UITableViewController这样的。UIBarButtonItem每行的附件视图中都有一个分组表视图。

在此处输入图像描述

- (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;
}

现在我试图UITableViewCell在点击相应的UIBarButtonItem. (来源

- (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。任何人都可以告诉我它有什么问题以及如何纠正这个问题?我真的很感激。

谢谢你。

4

5 回答 5

3

尝试使用view.superview.superview;而不是view.superview;

于 2013-08-06T07:00:17.920 回答
2

你这样设置 loginutton 标签

loginButton.tag=indexPath.section;

在点击事件中获取索引路径

- (void)login:(UIBarButtonItem *)button
{
UITableViewCell *cell= [tableView cellForRowAtIndexPath:button.tag];
}
于 2013-08-06T06:51:55.137 回答
1

cellForRowAtIndexPath

[loginButton setTag:indexPath.section];

登录:

   NSLog(@"secton: %d",(long int) [barButton tag]);
于 2013-08-06T06:51:51.263 回答
1

CellForRow:--

loginButton.tag=indexPath.row;

登录操作方法:--

 NSIndexPath *indexPath=[NSIndexPath indexPathForItem:button.tag inSection:0];
于 2013-08-06T06:55:58.667 回答
-1

为每个单元格中的每个barbutton项目设置不同的标签。然后根据按钮标签你可以找到它是哪个单元格。你可以这样访问

UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
于 2013-08-06T06:48:07.810 回答