【问题标题】:View-based NSTableView + NSButton基于视图的 NSTableView + NSButton
【发布时间】:2014-03-26 05:22:40
【问题描述】:

我有一个基于视图的 NSTableView,它使用 Cocoa 绑定来更改单元格中一些标签和图像的值。这一切都很好。但是,我想在单元格中添加一个按钮。我已经让按钮工作了,但它的操作方法只有按钮作为发送者,这意味着我不知道按钮所在单元格的内容。不知何故,我需要在按钮上存储一些额外的数据 - 在至少是按钮所在的行索引。我将 NSButton 子类化并在单元格中使用了我的子类,但 Interface Builder 不知道额外的属性,所以我无法绑定到它。如果我想在代码中绑定它,我不知道将传递给它的对象或键路径的名称。

我怎样才能让它工作?

【问题讨论】:

    标签: macos cocoa nstableview cocoa-bindings


    【解决方案1】:

    您可以在您的操作方法中使用 rowForView 来获取行值

    - (IBAction)doSomething:(id)sender
    {
            NSInteger row = [_myTableView rowForView:sender];
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 Interface Builder 中的 Identity 字段将 nib 中的表格单元格视图与代码中的实例相关联:

      此外,您必须在表视图的委托中实现- tableView:viewForTableColumn:row:。 (不要忘记在 IB 中连接代理)

       - (NSView*)tableView:(NSTableView*)tableView viewForTableColumn:  
      (NSTableColumn*)tableColumn row:(NSInteger)row
          {
              SSWButtonTableCellView *result = [tableView makeViewWithIdentifier:@"ButtonView" owner:self];
              result.button.title = [self.names objectAtIndex:row][@"name"];
              result.representedObject = [self.names objectAtIndex:row];
              return result;
          }
      

      我在我的NSTableCellView 子类中添加了representedObject 属性,这是我在上面的表格视图委托方法中设置的。

      您的自定义表格单元格视图稍后可以在其操作中使用该对象。例如:

      - (IBAction)doSomething:(id)sender
      {
          NSLog(@"Represented Object:%@", self.representedObject);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多