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?

8 Answers 8

24

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

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

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

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
    return NO;
}
1
  • 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 5, 2014 at 20:57
13

I think

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

is better than

- (NSIndexSet *)tableView:(NSTableView *)tableView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
1
  • 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, 2013 at 5:00
10

Swift 4.0

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

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:

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

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.

0

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

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.