How do you store child NSTableView settings for each parent item?
What is the best way to store an NSTableView
's settings (ideally leveraging its autosave capability) for each item in its parent NSTreeController
? I'm basically looking to reproduce the way iTunes stores the column settings for each playlist separately.
I've got a lot of columns, though, and wouldn't want to manually store every individual column size, etc. I'm also using Core Data, and would love a solution that used my existing data model to persist this information.
Update
I found some code (a Category
from Daniel Jalkut) that makes it easy to store and read an NSDictionary
representation of an NSTableView
's column settings. So I created a binary attribute in my Core Data entity that is now storing the settings for that entity's view.
What I'd like to be able to do is automatically retrieve the settings as each instance of the entity is selected. I had tried doing this with Key-Value Observation on my NSTreeController
, but it unfortunately doesn't give me a开发者_运维技巧dequate notification. I would like to know the item I'm switching away from, so that I can store the settings for the previous node before overwriting them with the new node's settings.
I tried observing @"selection"
, @"selectedObjects"
, and @"selectedIndexPaths"
, with every options
argument I could find, and none of them seemed to work properly. I receive notifications at the proper times, but without any useful information in the "change"
dictionary that the property change sends.
I decided to use the NSTableView+RSAutosaving category I linked to above (courtesy of Daniel Jalkut and Red Sweater Software), with some modifications to also store whether columns are hidden. I store this NSDictionary as a binary data attribute in my Core Data parent entity (and I created another one to separately store the sorting information).
Then, I have my window controller listen to two notifications from my NSOutlineView. On the @"NSOutlineViewSelectionIsChangingNotification"
notification, the NSTreeController's selection is the old node. I store the NSTableView's settings at this time. Then, on the @"NSOutlineViewSelectionDidChangeNotification"
notification, I read in the settings from the new node. This works like a charm.
精彩评论