Cocoa NSTableView-禁用行选择

Cocoa NSTableView-禁用行选择,cocoa,selection,nstableview,Cocoa,Selection,Nstableview,我有一个NSTableView,我想禁用行选择 表视图的列绑定到一个NSArrayController,数组的内容显示在表视图中 仅使用绑定如何做到这一点?我认为您需要使用TableViewDelegate并实现 - (NSIndexSet *)tableView:(NSTableView *)tableView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes 我想 - (BOOL

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

表视图的列绑定到一个
NSArrayController
,数组的内容显示在表视图中


仅使用
绑定如何做到这一点?

我认为您需要使用TableViewDelegate并实现

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

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

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

虽然前面的答案有效,但这是我更喜欢使用的另一个选项:

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

作为对后人的一个提醒

如果为ProposedSelection声明SelectionIndexes,则shouldSelectRow函数将被忽略。以防你像我一样想知道为什么我对shouldSelectRow的编辑没有效果


Swift 4.0

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

使用绑定:

在绑定的情况下,可以将布尔值绑定为Enabled。

如果sample中的值为true,那么它将是可选的,否则它将不可用。 这样,当所有其他工作都通过绑定完成时,我们不需要使用委托来禁用选择。

使用绑定:

另一种方法是在与表视图的属性检查器中的“选择”对应的复选框列表中选择“空”。默认情况下,这不会选择任何行

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

>通过此默认表视图选择从绑定检查器中删除


您可以在头文件中阅读:“为了更好地执行和控制选择,您应该使用tableView:SelectionIndexsforProposedSelection:”。如果您想禁用某些行而不是其他行,这实际上是您想要的方法