0
我如何总是可以在NSTableView中对列进行排序? 当我的视图加载时,数据并未排序,并且数据更新时也是如此。现在只有当我点击标题列时,数据才会被排序。总是排序NSTableView
这里是我的代码排序的数据上的标题栏,当点击:
在viewDidLoad中:
let descriptorName = NSSortDescriptor(key: "designation", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
tableview.tableColumns[0].sortDescriptorPrototype = descriptorName
委托FUNC:
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
let dataArrayMutable = NSMutableArray(array: self.data)
dataArrayMutable.sort(using: tableView.sortDescriptors)
self.data = (dataArrayMutable as NSArray) as! [Article]
tableview.reloadData()
}
问题解决:我必须在加载tableView之前对数据数组中的数据进行排序。以下是代码:
data = data.sorted(by: { $0.designation! < $1.designation! })
谢谢,我没有想到它! – user3722523