6

I'm trying to make a simple NSTableView (text only) where the cell views can be clicked upon to edit the text. All the tutorials and related questions here suggest that this is the automatic behaviour, but I can't get it.

I have no trouble linking my delegate and data-sources; I can populate all the cells programmatically, and I can figure out what to do with the new text that is entered upon editing. ... I just can't get the text box to open for editing!

(The NSTable columns are marked as editable in IB)

Thanks for any clues.

Joey FourSheds
  • 479
  • 1
  • 7
  • 18
  • OK, no answers, but could anyone confirm that at least this _should_ be possible, to click/doubleclick a cell in a NSTableView that opens the textfield for editing? I'm beginning to wonder if my Xcode installation is broken. In every tutorial I can find, this is taken as normal behaviour with no explanation how to create it. Thanks. – Joey FourSheds Feb 03 '13 at 12:39
  • 1
    Turns out it's not default behaviour, nor set-able globally in IB. You have to manually code each cell to be editable as you create them in your data-source/delegate. – Joey FourSheds Feb 04 '13 at 08:44
  • I have the same question. If you can provide more detail in an official answer, that'd be awesome. – Brett Feb 24 '13 at 20:25

2 Answers2

7

You can do this in Interface Builder by selecting the Text Field Cell, then using the Attributes Inspector, scroll down to the drop down menu entitled "Behavior" and choose "Editable".

This alone will enable you to be able to double-click on individual cells and have them become an editable TextField.

To make the edits you make have an effect, you will also have to implement the following method from the NSTableViewDelegate Protocol: - (void)tableView:setObjectValue:forTableColumn:row:

[See Apple Docs for this function] (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSTableViewDataSource/tableView:objectValueForTableColumn:row:)

As usual, this method will have to be implemented in the object that you have set as the delegate for the NSTableView object.

[See Apple Docs on delegates in case you want a review of them] (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18)

jLi
  • 133
  • 1
  • 7
  • I believe that although the links that you provided may have been consistent with your answer, but that now the Apple documentation says that the technique that you specified in your answer was only for cell-based table views. Now, Apple recommends that we use view-based table views and instead of using the delegate method, -(void)tableView:setObjectValue:forTableColumn:row: to set values that we use target/action. Can somebody tell me how to let the user edit values of a tableView and get those values back into my model, using target/action? – Kaydell Dec 09 '17 at 02:16
6

In the tableView:viewForTableColumn delegate method I had to specifically set each cell to be editable.

It appears to work without problem after a couple of weeks' use, but I still think there must be a less laborious method somehow.

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {


    NSTableCellView *cellView;
    if( [tableColumn.identifier isEqualToString:@"word"] )
    {
        wordCellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
        cellView = wordCellView;
        [cellView.textField setEditable:YES]; // Make Cell Editable!
     }
}
Joey FourSheds
  • 479
  • 1
  • 7
  • 18
  • Great idea, but in fact it's not necessary; see http://stackoverflow.com/a/28282459/341994. – matt Apr 11 '17 at 01:49