【问题标题】:NSTableViewCell selectedRow number for IBAction clickNSTableViewCell selectedRow number for IBAction click
【发布时间】:2016-04-05 00:25:26
【问题描述】:

我遇到了一个简单的问题,但尚未找到最佳解决方案。我有一个基于视图的 NSTableView,它从不同的 xib 加载它的单元格视图。我的表格视图是动态的,并且基于用户输入,我将动态添加和删除行,最终调整表格数据源。我的每个 NSTableCellViews 中都有一个按钮,我将 IBAction 单击处理程序链接到保存表视图的 NSView。我需要做的是获取在表格视图中单击的按钮的行号,以便处理逻辑。我能够成功地做到这一点:tableViewSelectionDidChange:(NSNotification *)notification

这是我的做法:

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
    NSTableView *tableView = [notification object];
    NSInteger selectedRow = [tableView selectedRow];
}

这对于实际单击该行的用户来说非常有效。现在,当我移动 NSButton IBAction 并将其链接到 NSView 时,如下所示:

- (IBAction)buttonClickHandler:(NSButton *)sender {
    NSInteger selectedRow = [self.tblView rowForView:sender];
    NSLog(@"%ld", (long)selectedRow);
}

我基于this 选定答案的这种方法。

我也试过这个:

- (IBAction)buttonClickHandler:(NSButton *)sender {

    id representedObject = [(NSTableCellView *)[sender superview] objectValue];
    NSLog(@"%@", representedObject);
}

//My configuration 

- (void)configureView {

    [self.view setFrame:[self bounds]];
    [self addSubview:self.view];

    [self.view setWantsLayer:YES];

    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.tblView.delegate = self;
    self.tblView.dataSource = self;





    [self.tblView setIntercellSpacing:NSMakeSize(0, 0)];

    [self.tblView registerNib: [[NSNib alloc] initWithNibNamed:@"ParentCellXib" bundle:nil] forIdentifier:@"ParentCell"];
    [self.tblView registerNib: [[NSNib alloc] initWithNibNamed:@"ChildCellXib" bundle:nil] forIdentifier:@"ChildCell"];
    [self.tblView registerNib: [[NSNib alloc] initWithNibNamed:@"HeaderCellXib" bundle:nil] forIdentifier:@"HeaderCell"];
}

但是表示的对象返回 null。如果值得一提,我已将我的文件所有者设置为包含 tableView 的视图,因此我可以链接 IBAction 并将 TableCellView 子类化为不同的类。但是,据我所知,我认为这不是问题的一部分。是否有一个简单的解决方案可以根据该单元格中的按钮单击可靠地为我提供 selectedRow 编号?我在上面尝试的两种方法分别返回 -1 和 null。

【问题讨论】:

  • 那么在哪里以及什么不起作用?你说什么有效,但不要说什么无效。
  • 我更新了帖子以进一步澄清问题所在。
  • rowForView 应该可以工作。 self.tblView 是否正确,senderself.tblView 的子视图吗? NSView 是self.tblView 的代表吗?基于视图的表视图中的对象只能连接到表视图的委托。
  • tblView 是视图的子视图,但 xib 是以编程方式加载的。 xibs 是表格单元格视图。并且按钮位于xibs本身上。我怀疑这可能与它有关。我将更新更多代码以显示上下文。

标签: objective-c cocoa nstableview appkit


【解决方案1】:

我会在 NSButton 的标签属性中设置行:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    SomeTableCellView *cell = [tableView makeViewWithIdentifier:@"cell" owner:self];
    if (cell == nil) {
        cell = // init some table cell view
        cell.identifier = @"cell";
    }

    cell.button.tag = row;
    [cell.button setTarget:self];
    [cell.button setAction:@selector(buttonAction:)];
}

- (IBAction)buttonAction:(id)sender {
    NSLog(@"row: %d", sender.tag);
}

【讨论】:

  • 看起来有人高兴地投了反对票,但没有说明原因。
【解决方案2】:

试试这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    yourCustomeCell *aCell;
    NSString *aStrIdentifier = @"yourIdentiFier";
    aCell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:aStrIdentifier];

    //you have to set your indexpath
    objc_setAssociatedObject(aCell.btnUpload_or_Add, @"objBtn", indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [aCell.YourButton addTarget:self action:@selector(yourButtonActiontapped:) forControlEvents:UIControlEventTouchUpInside];

    return aCell;
}

-(IBAction)yourButtonActiontapped:(UIButton *)sender{

    NSIndexPath *aIndPath =  objc_getAssociatedObject(sender, @"objBtn");
    NSLog(@"row:%@",aIndPath.row);
}

你也必须导入#import <objc/runtime.h>

在 IBAction 中获取行的另一种方法是 TAG,但 objc 是 TAG 的更好选择。

【讨论】:

    【解决方案3】:

    创建 UIButton 的子类并为按钮添加 NSIndexPath 属性。在 cellForRowAtIndexPath 方法中使用此按钮。将单元格的索引路径分配给按钮的索引路径。

    点击时,从其发送者那里获取索引路径。在您的情况下,该按钮的索引路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-16
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2011-02-22
      • 1970-01-01
      相关资源
      最近更新 更多