我正在尝试在NSTableView中设置排序。 NSArrayController绑定到一个数组:dynamic var dataArray = [Person]()
。
填充tableView到目前为止工作,但进入排序我陷入了这个错误。
我的设置是:
1:在IB中:ArrayController,Binding Inspector:
- Sort Descriptors bind to ViewController
- Model Key Path: self.customSortDescriptors
2:在IB中:tableView,Binding Inspector:
(这里我收到错误:“这个类不是关键的值编码兼容的关键customSortDescriptors。”)
- Sort Descriptors bind to ArrayController
- Controller Key: arrangedObjects
- Model KEy Path: customSortDescriptors
3:在IB中:列“名称”,属性检查器:
- Sort Key: name
- Selector: caseInsensitiveCompare:
在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))
}
}
在我从上面设置第二步之前,一切正常运行:2: in IB: tableView, Binding Inspector:
然后我收到错误:this class is not key value coding-compliant for the key customSortDescriptors.
人员类:
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()
}
}
这是一个演示项目:https://drive.google.com/file/d/0BwRghT926ZpyMVhZMHJqTGFOS3c/view