21

I have a NSTableView and I want to disable row selection.

The columns of the table view are bound to a NSArrayController and the content of the array does show up in the table view.

How can I do this just using bindings?

Nekto
  • 17,837
  • 1
  • 55
  • 65
ericg
  • 8,413
  • 9
  • 43
  • 77

8 Answers8

23

I think you'll need to use a TableViewDelegate and implement

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

While the previous answers work, this is another option which I prefer to use:

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
    return NO;
}
David Douglas
  • 10,377
  • 2
  • 55
  • 53
  • 1
    This is actually the method you'd want if you want to disable some rows and not others from being selected – Chris R Jun 05 '14 at 20:57
13

I think

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

is better than

- (NSIndexSet *)tableView:(NSTableView *)tableView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
Nicolas Hognon
  • 328
  • 3
  • 10
  • 3
    You can read in the header file: "For better performance and better control over the selection, you should use tableView:selectionIndexesForProposedSelection:." – Stephan Aug 20 '13 at 05:00
9

Swift 4.0

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
    return false
}
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
3

As a note to posterity...

If you declare selectionIndexesForProposedSelection, then shouldSelectRow function will be ignored. Just in case you're wondering like I did why my edits to shouldSelectRow had no effect...

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

arinmorf
  • 1,374
  • 16
  • 27
1

Using Binding:

Another approach is select 'empty' in the list of checkboxes corresponding to 'Selection' in Attribute Inspector of table view. This will not select any rows by default.

In addition to this, make the highlight option to 'None'.

screenshot of attribute inspector

pranav
  • 499
  • 5
  • 6
0

Using Binding:

In case of binding, you can bind a boolean value with Enabled. binding inspector

if the value in sample is true, it would be selectable or else, it wouldnt be. This way, we dont need to use delegates just for disabling selection when all other stuffs are done through binding.

pranav
  • 499
  • 5
  • 6
0

enter image description here> Through this by default TableView selection remove from bind inspector

enter image description here

enter image description here

Sawan Cool
  • 158
  • 3
  • 13