I have had this question on stackoverflow for a couple days now and no one there can figure it out, so I thought I would try here:
I have a tableView with some custom cells. The columns are sortable with NSSortDescriptor. But the tableView.reloadData() does not refresh the custom cells.
[code]
extension DeviceListViewController:NSTableViewDataSource, NSTableViewDelegate{
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?{
var result = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
if (tableColumn?.identifier)!.rawValue == "Name" {
result.textField?.stringValue = homedevices[row].name
result.imageView?.image = homedevices[row].image
} else if (tableColumn?.identifier)!.rawValue == "DeviceGroup" {
let buttoncell = result as! ButtonCellView
buttoncell.selectedItem = homedevices[row].button
result = buttoncell
} else if (tableColumn?.identifier)!.rawValue == "Brightness" {
let slidercell = result as! BrightnessSliderCellView
slidercell.sliderValue = homedevices[row].brightness result = slidercell
}
return result
}
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
let dataArrayMutable = NSMutableArray(array: homedevices)
dataArrayMutable.sort(using: tableView.sortDescriptors)
homedevices = dataArrayMutable as! [HomeDevice] tableView.reloadData()
}
}
[/code]
I am not sure why this doesn't work, but sorting any column results in the custom cells looking exactly the same until I close the window and reopen it. How can I fix this? Or is there a way to do a full reload of the view as if closing and reopening the window?
EDIT: In case this is confusing, I will try to rephrase it. I would like to be able to reload the data in the tableView for all of the cells that are in the table. Currently, I have to close the window and reopen it to refresh all of the table cells.
DAY2: The lastest attempt involves doing something like this:
[code]
let indexSet = IndexSet(integersIn: 0..<homedevices.count)
tableView.removeRows(at: indexSet, withAnimation: .effectFade)
tableView.reloadData()
[/code]
now, all the cells refresh, but the sorts are still messed up sometimes. I can actually make this kind of work now, with all columns displaying correct data, but only if I only implement sort on one column. If I use multiple columns for sorting, it still doesn't work right. If anyone has any idea of how to sort an NSTableView with sub-classed cells in Swift 4, please let me know a better direction. This is so strange.