reloadData in NSTableView but keep current selection

后端 未结 3 913
时光说笑
时光说笑 2021-02-12 17:20

I have anNSTableView showing the contents of a directory. I watch for FSEvents, and each time I get an event I reload my table view. Unfortunately, the current sele

3条回答
  •  清酒与你
    2021-02-12 17:51

    Swift 4.2

    Create an extension and add a method which preserves selection.

    extension NSTableView {
    
        func reloadDataKeepingSelection() {
            let selectedRowIndexes = self.selectedRowIndexes
            self.reloadData()
            self.selectRowIndexes(selectedRowIndexes, byExtendingSelection: false)
        }
    }
    

    Do this in case you use the traditional way of populating table views (not NSArrayController).

提交回复
热议问题