NSTableView - 禁用行选择

问题描述 投票:18回答:7

我有一个NSTableView,我想禁用行选择。

表视图的列绑定到NSArrayController,并且数组的内容确实显示在表视图中。

我怎么能用bindings做到这一点?

cocoa selection nstableview
7个回答
23
投票

我想你需要使用TableViewDelegate并实现

- (NSIndexSet *)tableView:(NSTableView *)tableView 
    selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes

15
投票

虽然之前的答案有效,但这是我更喜欢使用的另一种选择:

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
    return NO;
}

13
投票

我认为

- (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTableView
{
  return NO;
}

比...更好

- (NSIndexSet *)tableView:(NSTableView *)tableView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes

7
投票

Swift 4.0

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
    return false
}

1
投票

作为后人的一个注释......

如果声明selectionIndexesForProposedSelection,那么将忽略shouldSelectRow函数。万一你想知道我为什么我对edSelectRow的编辑没有效果...

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/NSTableViewDelegate/tableView:selectionIndexesForProposedSelection


0
投票

使用绑定:

在绑定的情况下,您可以使用Enabled绑定布尔值。 binding inspector

如果样本中的值为true,则可以选择,否则就不会。这样,当所有其他东西都通过绑定完成时,我们不需要使用委托来禁用选择。


0
投票

使用绑定:

另一种方法是在表视图的Attribute Inspector中与'Selection'对应的复选框列表中选择'empty'。默认情况下,这不会选择任何行。

除此之外,将突出显示选项设为“无”。

screenshot of attribute inspector

© www.soinside.com 2019 - 2024. All rights reserved.