views:

84

answers:

2

I have an NSTableView and I want to do something whenever the selectedCell element changes.

So, my table view is called tableView, and this is what I want to observe:

[tableView selectedCell]

I tried using key-value observing, but that didn't seem to work, or maybe I was doing it wrong. Any ideas?

A: 

Most properties of Cocoa's own classes are not observable. If a property is observable, the documentation for it will explicitly say so; if the documentation doesn't say a property is observable, assume it isn't.

Furthermore, properties that don't exist are doubly not observable. The documentation for NSTableView and NSOutlineView both mention no method named “selectedCell”. You should assume there isn't one.

If you want to know when the user selects a different row, be the table view's delegate; it sends delegate messages for that, if you'll respond to them.

Peter Hosey
selectedCell is a method of NSControl, which NSTableView inherits from. Is there some other way I can be notified that the cursor is in a new cell in an NSTableView?
Alex G
Ah, my mistake. I always forget that table views have the control nature. Even so, I'm not sure `selectedCell` works on a table view. As for detecting when the user is editing a cell, there are delegate methods for that, too.
Peter Hosey
By the way, if you're just trying to make your data editable, working on the cell-by-cell level is not the way to do it. Either be the data source and implement `tableView:setObjectValue:forTableColumn:row:`, or bind your table column to a controller holding an array and make sure that the controller allows editing and the model objects are mutable (which they should be, unless you made them otherwise).
Peter Hosey
A: 

NSTableView will use one and only one dataCell object for each column. selectedCell is the wrong way. You can use selectedColumn to get the selected column and then ask for its dataCell.

And: I guess you are searching for NSTableView delegate methods tableViewSelectionDidChange: and tableViewSelectionIsChanging:

cocoafan