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()
}
問題はを解く:I tableViewをロードする前にデータ配列内のデータをソートしなければなりませんでした。コードは次のとおりです。
data = data.sorted(by: { $0.designation! < $1.designation! })
ありがとう、私はそれについて考えなかった! – user3722523