PDA

View Full Version : NSTableView behaviour question




gekko513
Apr 28, 2006, 08:16 AM
I find it difficult to control some of the behaviour of an NSTableView in Cocoa. Maybe I'm just missing something.

I want to change three things:
1. To disable user selectable sorting on the columns.
2. To disable column AND row selection in the table view. I don't want the user to be able to select anything.
3. To disable editing of the text in all cells.

Can I do this? How?



robbieduncan
Apr 28, 2006, 08:30 AM
I think you can set all of these attributes in Interface Builder. Unfortunatly I'm not on a Mac now so I can't post a screenshot of the boxes you want...

http://www.oreilly.com/catalog/learncocoa/apple/LCoc_1014.gif You can column sizing and reordering here. You need to turn off cell editability on a per column basis I think.

whooleytoo
Apr 28, 2006, 08:40 AM
I want to change three things:
1. To disable user selectable sorting on the columns.


Not certain, if you disable "Column Selection" does that do it? Otherwise, perhaps implementing tableView:shouldSelectTableColumn: in the NSTableView's delegate object, and returning NO might do it.


2. To disable column AND row selection in the table view. I don't want the user to be able to select anything.

One way, is to implement 'selectionShouldChangeInTableView:' in the delegate object, and return NO.


3. To disable editing of the text in all cells.


Select each column in turn (by repetitively double clicking them), then open the inspector and uncheck "Editable".

gekko513
Apr 28, 2006, 09:11 AM
I did it!

1. The solution wasn't very intuitive. I found it here (http://homepage.mac.com/mmalc/CocoaExamples/controllers.html):
Disabling sorting in a tableview

(Courtesy of James Dempsey.)

To disable sorting in a tableview you need to do the following:

- Select the table view itself and inspect its bindings.

- You will see three Table Content bindings for the table view:
content
selectionIndexes
sortDescriptors
- Bind the content to:
Bind To: Your Array Controller
Controller Key: arrangedObjects
Model Key Path: <leave this blank>
Bind selectionIndexes to:
Bind To: Your Array Controller
Controller Key: selectionIndexes
Model Key Path: <leave this blank>
- Leave sortDescriptors unbound

2. Thanks whooleytoo, the - (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex delegate method did it. I was trying to make a value transformer for the selectionIndexes bindings to zero out the selection. That worked in a non-desirable way. The row did go selected (blue) on mouse down and was unselected on mouse up.

3. The column header "editable" attribute did the trick. Thanks to both of you.