NSTableView - Disable Row Selection
Asked Answered
S

8

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?

Stopper answered 2/9, 2011 at 15:5 Comment(0)
C
24

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

- (NSIndexSet *)tableView:(NSTableView *)tableView 
    selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
Chacon answered 2/9, 2011 at 19:57 Comment(0)
N
23

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

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
    return NO;
}
Nanoid answered 17/1, 2014 at 22:38 Comment(1)
This is actually the method you'd want if you want to disable some rows and not others from being selectedInformation
C
13

I think

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

is better than

- (NSIndexSet *)tableView:(NSTableView *)tableView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
Corot answered 30/7, 2012 at 21:43 Comment(1)
You can read in the header file: "For better performance and better control over the selection, you should use tableView:selectionIndexesForProposedSelection:."Nettle
A
10

Swift 4.0

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
    return false
}
Augustusaugy answered 25/4, 2018 at 17:39 Comment(0)
R
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:

Rescission answered 3/8, 2015 at 22:13 Comment(0)
G
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

Geraldo answered 23/4, 2019 at 13:3 Comment(0)
G
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.

Geraldo answered 10/4, 2019 at 19:9 Comment(0)
A
0

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

enter image description here

enter image description here

Ariella answered 7/2, 2020 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.