reloadData in NSTableView but keep current selection

后端 未结 3 1428
生来不讨喜
生来不讨喜 2021-02-12 17:18

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:59

    Well, you can save selection before calling reloadData and restore it after that.

    NSInteger row = [self.tableView selectedRow];
    [self.tableView reloadData];
    [self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
    

    This worked for me in some cases. But if you insert some items BEFORE the selected row, you should andjust your row variable by adding count of added items to it.

提交回复
热议问题