NSTableView: selecting non-editable CELLS and not ROWS

1.1k views Asked by At

I've tried numerous combinations of suggested options from other posts, but I can't seem to get a cell-bassed NSTableView populated with non-editable NSTextFieldCell to select the CELL and not the row.

I've tried:

    [[col dataCell] setEditable:NO];
    [[col dataCell] setSelectable:YES];

    [col setEditable:YES];

and tried delegates:

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex;

and

- (BOOL)shouldFocusCell:(NSCell *)cell atColumn:(NSInteger)column row:(NSInteger)row;

The reason for permitting selecting of the cell (and not editing the text) is that I allow the user to double-click on the cell to perform a navigation function. Double-clicking works great, but the user should be able to see that a single click (or even the first click) selects the cell. But all I get is the row being selected, and even if the delegate inhibits this, no cell selection.

Is the only way to do this to force the cell to draw its background in the selection highlight color? (seems hackish).

BTW, COLUMN selection IS permitted, too. That presently works.

Anyone have any experience with this issue? Or some sage guesses?

Stephen

1

There are 1 answers

3
Peter Hosey On BEST ANSWER

It sounds like you're trying to make a spreadsheet. That's not what a table view is.

A table view is a list of items, one per row, showing one or more aspects of each item, one per column.

In that context, it seldom makes sense to select but not edit only one property of a single item. There may be use cases for it (might be handy to be able to select and copy only the title of a track in iTunes, for example), but it's not something NSTableView and NSOutlineView accommodate.

A spreadsheet is something very different. Every cell is a peer to every other; neither axis is necessarily subordinate to the other. A spreadsheet can be used as a table (e.g.: item, price, quantity, subtotal), but is not bound to the properties-of-items paradigm of NSTableView.

NSTableView's API bears this out. Note that a table view has selectedRowIndexes and selectedColumnIndexes, but no way to enumerate the column,row pairs that would identify selected individual cells.

You're best off implementing a custom view. You'd probably do well to emulate NSTableView's data source protocol—you could have spreadsheetView:objectValueForColumnIndex:rowIndex:, for example—but otherwise, you have a lot of work ahead of you, particularly to implement editing.

Long-term, though, it's the best way, and the only way to get individual-cell selection.