Result should be an settings panel with an OutlineView
and "add ite开发者_如何学编程m", "add group" and "delete" buttons. The buttons add entries to an NSOutlineView
. The data is stored in a NSMutableDictionar
y (or whatever is suitable). Sorting/DragDrop enabled for the OutlineView
.
What is the best or most comfortable way for this (and write less code)?
Modifying NSMutableDictionary
, NSOutlineView
refreshes from NSMutableDictionary
?
Modifying NSOutlineView
, Result is stored in NSMutableDictionary
?
... NSTreeController
?
... CoreData
?
What's best practice for that?
Thanks in advance!
This is a pretty broad question. You should always store your model data in a model object of some kind, be that a Core Data entity, an NSMutableDictionary
or a custom object of your own creation. You should definitely NOT be storing the data in an NSTreeController
or NSOutlineView
instance, these are not model objects.
If you're using Core Data for the rest of your app and you need to persist the data that is manipulated by the outline view then this is a good choice, but it might be overkill if you have only simple requirements.
To control what is displayed in the outline view you can either use NSTreeController
or your own controller object that responds to the NSOutlineView
datasource and delegate protocols. In practice you might use both, as some things such as whether or not an item is a group item can only be controlled by the NSOutlineView
delegate methods.
In my personal experience I've found that NSTreeController
can be very difficult to deal with for anything beyond very simple tasks and I now longer use it, I find it's much simpler to just use the datasource methods in my own controller.
As far as modifying the contents of the outline view, you should always modify the model via a controller, you should never update the view directly. You would implement methods such as -add:
in your controller or use the -add:
method of NSTreeController
if you're using it.
Your view's controller should then detect the change in the model and ask the view to update. The view controller and model controller can be the same object but they don't have to be. Key-Value Observing is a useful technology that can inform your controller of a change in the model.
Here's some sample code from Apple that you might find useful:
http://developer.apple.com/mac/library/samplecode/SourceView/
http://developer.apple.com/Mac/library/samplecode/AbstractTree/
精彩评论