Creating row groups in NSTableView using CoreData NSArrayController support

Say I have a bunch of article objects stored in a basic data model. Each article supports the reading progress attribute, which indicates which part of the article has been read by the user. After the user has fully read the article, the article is marked as "Reading." Prior to that, it would have been marked as "Unread."

Now I have an NSTableView associated with an NSArrayController containing all article objects stored using CoreData. I want it to display all unread articles in the Unread group and all read articles in the Reading group.

I know about the method

- (BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row 

in an NSTableViewDelegate. However, this indicates that we should have real lines with the words "Read" and "Unread" in the NSArrayController, which will be promoted to the group. Since NSArrayController contains article objects extracted from CoreData, how do I do this?

NB: This is my first question here, and I'm new to Cocoa, please, easy on me :)

+7
source share
2 answers

I suggest using NSOutlineview and NSTreeController to create groupings instead of NSTableView and NSArrayController.

0
source

You must manually populate the NSArrayController with instances of a custom class that exactly contain the contents of the NSTableView that you want to execute, including group rows. The controller must be filled in the correct order. Therefore, it should start with a group header object, then with some data objects, a new header object, etc. A custom class must contain the following properties:

The string value for the group header. Nil when the string is not a group header.

Boolean value isGroupHeader. Required for a simple tableView: isGroupRow: implementation.

An NSManagedObject value for your regular data. Nil when the string is the group header.

In doing so, you have all the tools to implement all NSTableView delegates to get a properly grouped view of the table.

0
source

All Articles