【问题标题】:Odd behavior of rowForView and selectRowIndexes in NSOutlineViewNSOutlineView 中 rowForView 和 selectRowIndexes 的奇怪行为
【发布时间】:2014-02-13 17:43:24
【问题描述】:

我的应用中有一个源列表视图,它设置为基于视图。然后我通过这种方法在我的单元格中添加了一个徽章/按钮;

第一个子类 NSTableCellView,为 NSButton 添加一个 IBOutlet 和一个 @synthesize/@property 声明它。然后打开 NSTableCellView 在 Interface Builder 中应该有一个徽章。将它的类设置为您的 新创建的子类并为其添加一个按钮。将按钮样式设置为 “inline”,它是“switch”的类型。

现在再次选择 NSTableCellView 并连接 NSButton IBOutlet 到您添加的按钮。就是这样。您现在可以调用例如[[单元格视图 button] setTitle@"123"]] 将行徽章标签设置为 123 或任何 任意字符串。

这很好用,但点击徽章并没有真正选择该行,所以我为按钮添加了一个点击处理程序。

- (IBAction)sourceListBadgeClicked:(id)sender {
    NSUInteger selectedRow = [self.outlineView rowForView:sender];
    if (selectedRow == -1) {
        return;
    }

    NSLog(@"click row: %lu", selectedRow);

    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
    [self.outlineView selectRowIndexes:indexSet byExtendingSelection:NO];
}

我可以看到处理程序被调用,但 selectedRow 始终返回为 0,并且 selectRowIndexes 实际上并没有选择该行(我硬编码为 1 只是为了测试)。我在如何这样做?

【问题讨论】:

    标签: cocoa


    【解决方案1】:

    事实证明,为您尝试操作的视图绑定 IBOutlet 确实很有帮助。我的新手错误。

    【讨论】:

      猜你喜欢
      • 2014-03-17
      • 2019-03-21
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多