Sectioned NSTableView using NSArrayController
Asked Answered
A

3

9

I'm trying to create a sectioned NSTableView using NSArrayController and cocoa bindings. I'm searching for a similar approach like with NSFetchedResultsController in iOS, where you can set a section key path. I want to make something similar with NSArrayController.

I give you an example: I have different tasks. Each task has one of three different priorities, low, medium or high. The tasks also has attributes like title, description, date, etc. I want to section or group the tasks by priority (the section key path here is the priority).

You can I solve this problem with bindings and NSArrayController? Do I need multiple NSArrayController or just one?

Alek answered 26/3, 2011 at 14:58 Comment(1)
Try this developer.apple.com/library/mac/samplecode/TableViewPlayground/…Manslayer
N
7

Though NSTableView can render group rows, unfortunately it depends on the delegate or data source to flatten the hierarchy. It doesn't understand alternate data source semantics like UITableView's UITableViewStyleGrouped, nor does NSArrayController have equivalents to NSFetchedResultsController's section methods.

One option is NSOutlineView which is designed for displaying arbitrarily deep hierarchical data, but it probably creates more work than necessary for a simple two-level structure. (You can bind an outline view to NSTreeController but the tree controller is poorly documented, and to boot, it's badly designed.)

So, here's what I suggest:

  • Write your own controller class which implements NSTableViewDelegate and NSTableViewDataSource.
  • Use a single array controller which sorts by priority, and write your own code to offset the row indexes to accommodate the section headings.
  • Return YES from tableView:isGroupRow: on the section headings.
  • Don't use bindings for this one.

You could also look for a reusable, bindings-capable controller from a third party.

Niedersachsen answered 13/3, 2012 at 17:31 Comment(1)
It is possible to use bindings for the NSTableCell fields in conjunction with self made section headings! You should implement NSTableViewDelegate methods tableview:isGroupRow: and tableView:rowViewForRow:. You can leave out implementation of tableview:viewForTableColumn:row, it can be done via bindings.Zeidman
P
0

You could try implementing a sectioned NSTableView using the tools mentioned in this answer:

https://mcmap.net/q/1318107/-uitableview-like-nstableview

Puzzle answered 7/9, 2012 at 8:48 Comment(0)
K
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:

  1. String value for the group header. Is nil when the row is no group header.
  2. Boolean value isGroupHeader. Needed for an easy tableView:isGroupRow: implementation.
  3. 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.

Khalsa answered 16/10, 2017 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.