Create group rows in NSTableView using CoreData backed NSArrayController
Asked Answered
N

2

8

Say I have a bunch of Article objects stored within the core data model. Each article maintains, a reading progress attribute which denotes how much of the article has been read by the user. Once the user has read the article fully, the article is marked as "Read". Before that, it would be marked "Unread".

Now I have an NSTableView bound to a NSArrayController containing all the Article objects stored using CoreData. I want it to display all unread articles under an "Unread" group, and all read articles under the "Read" group.

I know about the method

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

in the NSTableViewDelegate. However, this insinuates that we must have actual rows saying "Read" and "Unread" within the NSArrayController that will be promoted to group. Since the NSArrayController contains Article objects pulled from CoreData, how do I go about doing this?

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

Nostril answered 8/6, 2013 at 14:57 Comment(3)
2 years old and still no answer. Did you find a solution in the meantime?Ruffianism
I am also looking for such feature but found no solution. Were you able to do so. If yes, could you let me know how to achieve that.Deathblow
One solution is to use 3rd party FRC like this one github.com/konstantinpavlikhin/KSPFetchedResultsControllerCammycamomile
C
0

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

Curvy answered 7/6, 2016 at 11:14 Comment(0)
M
0

You have to fill NSArrayController manually with a custom class instances that exactly holds the content of the NSTableView you want to accomplish, including the group rows. The controller must be filled in the correct order. So it should start with a group header object, then some data objects, a new header object, and so on. The custom class should contain the following properties:

String value for the group header. Is nil when the row is no group header.

Boolean value isGroupHeader. Needed for an easy tableView:isGroupRow: implementation.

NSManagedObject value for your regular data. Is nil when the row is a group header.

With this you have all the tools to implement all NSTableView delegates to get a properly grouped table view.

Monetary answered 16/10, 2017 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.