UICollectionView List Style on Mac Catalyst: Prevent the Collection View From Changing the Selection when the Selected Row's Parent is Collapsed?

I'm using UICollectionView with a list style on Mac (looks like NSOutlineView in a sidebar). I have expandable rows.

So this is the behavior I'm after:

-If the user has a row selected, but decides to collapse the selected row's parent, I want to maintain the current selection.

Instead this is what happens by default:

-If a row selected and I collapse the parent the collection, the collection view automatically selects another row (in my case the first row at index path 0-0).

Just because the user collapses the parent doesn't mean the selection should change. For instance in Xcode's Navigator I can have a file selected in a Group and collapse the group without modifying the current selection.

Actually I get the behavior I expect by default as long as the selected row isn't also focused (the collection view is configured to have the selection follow focus, which is something I want).

Only thing I can think of is to manually implement the expandable row's UICellAccessoryOutlineDisclosure actionHandler, set selectionFollowsFocus to NO before I collapse the row, then set selectionFollowsFocus to YES after the animation finishes. Feels like this is way more work then it should be though.

Accepted Answer

Actually I get the behavior I expect by default as long as the selected row isn't also focused (the collection view is configured to have the selection follow focus, which is something I want).

Only thing I can think of is to manually implement the expandable row's UICellAccessoryOutlineDisclosure actionHandler, set selectionFollowsFocus to NO before I collapse the row, then set selectionFollowsFocus to YES after the animation finishes. Feels like this is way more work then it should be though.

Also not sure why focus moves to the first row (0,0) when another row is collapsed with the selected and focused row. If the collapsed parent (just clicked) isn't selectable but is focusable the focus should move to the clicked row. IMO it shouldn't jump to index path 0,0. Overriding this behavior is not as straightforward since the focus API gives developer's limited control. I have to hold the clicked index path in a property and return it from -indexPathForPreferredFocusedViewInCollectionView:

UICollectionView List Style on Mac Catalyst: Prevent the Collection View From Changing the Selection when the Selected Row's Parent is Collapsed?
 
 
Q