Reputation: 343
I´m trying to setup sorting in NSTableView. NSArrayController is bound to an Array: dynamic var dataArray = [Person]()
.
Populating the tableView is working so far, but coming to sorting I´m stuck in this error.
My setup is:
1: in IB: ArrayController, Binding Inspector:
- Sort Descriptors bind to ViewController
- Model Key Path: self.customSortDescriptors
2: in IB: tableView, Binding Inspector:
( Here I´m getting error: "this class is not key value coding-compliant for the key customSortDescriptors.")
- Sort Descriptors bind to ArrayController
- Controller Key: arrangedObjects
- Model KEy Path: customSortDescriptors
3: in IB: Column "Name", Attributes Inspector:
- Sort Key: name
- Selector: caseInsensitiveCompare:
In ViewController:
class ViewController: NSViewController {
@IBOutlet var arrayController: NSArrayController!
@IBOutlet var tableView: NSTableView!
dynamic var dataArray = [Person]()
dynamic var customSortDescriptors = [NSSortDescriptor(key: "name", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))];
override func viewDidLoad() {
super.viewDidLoad()
dataArray.append(Person(name: "Noah", familyName: "Vale", age: 72))
dataArray.append(Person(name: "Sarah", familyName: "Yayvo", age: 29))
dataArray.append(Person(name: "Shanda", familyName: "Lear", age: 45))
}
}
Everything works without errors until I set the second step from above: 2: in IB: tableView, Binding Inspector:
Then I´m getting error: this class is not key value coding-compliant for the key customSortDescriptors.
Person class:
class Person : NSObject {
var name:String
var familyName:String
var age = 0
override init() {
name = "name"
familyName = "family"
super.init()
}
init(name:String, familyName:String, age:Int) {
self.name = name
self.familyName = familyName
self.age = age
super.init()
}
}
This is a demo project: https://drive.google.com/file/d/0BwRghT926ZpyMVhZMHJqTGFOS3c/view
Upvotes: 1
Views: 732
Reputation: 285190
The error occurs because the array controller doesn't have a property customSortDescriptors
Bind tableView.sortDescriptors
to arraycontroller.sortDescriptors
(controllerKey!).
However if you want to enable column sorting by clicking on the header add values for Sort Key
and Selector
in the table column:
In this case you need only the binding from table view to array controller as in the first image.
Upvotes: 2