Search code examples
cocoamonomac

how to make a change to an NSOutlineViewDataSource trigger a refresh of other items


I am new to Cocoa programming (but relatively experienced with other UI frameworks) and can't tell exactly where I am going wrong in the following situation.

I have a 'tree' of data that is wrapped by a NSOutlineViewDataSource. The items are very basic read/write (think checkboxes). However, when

setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item

is called, the changes I make do not just affect the single item in question, but can propagate to other items i.e. checking one item might cause other items in the tree to select or deselect. At the data model level, I have got this working fine. My problem is somehow forcing the outline to refresh so that these other changes appear in the UI.

So basically, when setObjectValue is called the individual cell that is changed is refreshed, but I need (potentially) the whole tree to refresh in the UI.

What is the best practice mechanism for doing something like this? Is there an event on the NSOutlineView I should be listening for to then trigger a refresh. Should my NSOutlineViewDataSource be raising an event that the view listens for? Or is my basic approach entirely wrong?

(I am actually doing this with Monomac but I think any answer applies to cocoa in general)


Solution

  • Andrew - the easiest way to do this is to look into KVO programming. If a particular item you want to watch is KVO compliant, you can set up a watcher on it with "addObserver" Then you implement the delegate function "observeValueForKeyPath" in the observer class and can force the changes to the UI when that function is notified of a change to the value you're watching. Not everything is KVO compliant, so your mileage my vary depending upon the nature of the thing you're wanting to monitor for changes. But if you're watching a property on a custom object, it will definitely be fine to use KVO.

    The KVO programming guide is here:

    https://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html