1

I have constructed a window with a single view-based NSTableView, programmatically (code-only, without XIB) following this, which is backed by an Array in swift. I instead want to back it by an NSArrayController so had it simply by:

var controller = NSArrayController()

and modify its contents, when needed, using one of its proper methods:

controller.addObject(item)
controller.removeObjectAtArrangedObjectIndex(index)
controller.insertObject(item, atArrangedObjectIndex: atIndex)

and bound it with:

tableView.bind(NSContentBinding, toObject: controller, withKeyPath: "arrangedObjects", options: nil)
tableView.bind(NSSelectionIndexesBinding, toObject: controller, withKeyPath:"selectionIndexes", options: nil)
tableView.bind(NSSortDescriptorsBinding, toObject: controller, withKeyPath: "sortDescriptors", options: nil)

Cell views are created in the delegate with:

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
        // ... erased code for finding cellIdentifier and text.. 
        view = tableView.makeViewWithIdentifier(cellIdentifier, owner: nil) as? PLTextCellView
        if view == nil {
            print("new view for text: "+text)            
            view = NSTextField()
            view!.identifier = cellIdentifier
        }
        view!.stringValue = text        
        return view
}

Add/remove and number of items work ok but object id's are displayed, in the cells, between '<' and '>', instead of the values I assign as text. This was working fine before the NSArrayController. What do I need to do?

Notes: This question gives an -incomplete- answer which give An instance of ...item... was deallocated while key value observers were still registered with it. error. I also did bind my table according to this answer which only states a necessity to bind the cells also, manually. I am asking how?

Community
  • 1
  • 1
user3648895
  • 395
  • 10
  • 20
  • Possible duplicate of [Create view based NSTableView programmatically using Bindings in Swift](http://stackoverflow.com/questions/34583638/create-view-based-nstableview-programmatically-using-bindings-in-swift) – Willeke Jun 25 '16 at 11:31
  • @Willeke : It is not; edited and clarified accordingly, see the notes. – user3648895 Jun 25 '16 at 18:50
  • `NSTableCellView` and `viewForTableColumn` are used by view based tableviews. Subclasses of `NSCell` and `dataCellForTableColumn` are used by cell based tableviews. – Willeke Jun 25 '16 at 19:56
  • "Note that a cell view’s identifier must be the same as its table column’s identifier for bindings to work." – Willeke Jun 25 '16 at 20:05
  • @Willeke : Had thought of it once and had already done, without any effect... – user3648895 Jun 25 '16 at 20:08

0 Answers0