morphinewan
morphinewan

Reputation: 444

How to keep the selected row state after collapsing the NSOutlineView Group

I want to implement a side bar working like Finder.app. But I have no idea how to make it keeping the row selected state after collapsing and expanding the group.

Any idea or suggestion will be appreciated.

Upvotes: 3

Views: 1283

Answers (1)

Renfei Song
Renfei Song

Reputation: 2960

You will have to implement it yourself. The reason why system don't preserve the selection for you is that when an item is collapsed, all of its subitems are actually released, so they won't be exist at all.

The solution is pretty easy and straight-forward.

  1. Set the outline view to allow empty selection (either in IB or via code). This will prevent system from automatically selecting another item after previously selected item is collapsed.
  2. Save the selected item in an ivar, and update it in response to user interaction. To do this, you may want to implement the -outlineViewSelectionDidChange: delegate method. Note that you should always ensure that [[notification object] selectedRow] != -1 is satisfied before updating it, since collapsing the selected item will cause this message to be sent with a selectedRow of -1.
  3. In the -outlineViewItemDidExpand delegate method, if outlineView.selectedRow is -1, consult your ivar and manually resume the selection by sending a -selectRowIndexes:byExtendingSelection: message to your NSOutlineView.

Upvotes: 8

Related Questions