Create group rows in NSTableView using CoreData backed NSArrayController

丶灬走出姿态 提交于 2019-12-05 21:18:00

问题


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 :)


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/17000753/create-group-rows-in-nstableview-using-coredata-backed-nsarraycontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!