Search resets NSArrayController selection to none
-
05-07-2021 - |
题
I've got an NSTableView
bound to an NSArrayController
via content
and selection indexes
. All great so far - content displayed, etc.
Now an NSSearchField
is bound to the array controller via filterPredicate
and the property of the array content instances that's to be searched.
Searching/filtering the table view works great; table view showing only matching entries.
However, searching resets the selection on the NSTableView
if the existing selection isn't in the search results. Worse, not only during the search but after ending the search there's no selection on the table view.
The NSArrayController
is set up to Avoid Empty Selection.
Yet, debugging the array controller's selection indexes
shows that searching resets them to an empty set. Don't quite know what to make of it..
Any hints on how to properly configure bindings in this scenario to really prevent an empty selection much appreciated!
解决方案
Unfortunately array controllers don't track and restore the selection as their arranged objects change. You'll have to do this yourself in code. You can keep track of the current selection by using KVO to observe the selection on the array controller. You can also observe the controller's arranged objects to know when it changes as a result of filtering. Upon every change just set the current selection back to the tracked value (assuming its still in arranged objects) or set the selection to a new value.