The Hit List Diary #17.1 – Clearing Selection After a Modal Dismissal

Remember this code from Castro for interactively clearing a table’s selection?

Previously, I looked at mixing this with multiple selection. Today, a different edge case to consider:

  1. Tap on a table cell to select it
  2. That triggers modally presenting a panel atop the table (rather than pushing onto the navigation stack)
  3. The panel can then be dismissed, potentially interactively

Using the above code, you’d find that during an interactive dismissal, the table row gets deselected, yes, but it’s not synchronised with the interaction.

The trick is interactive transitions need to “capture” any changes to the layer tree that are part of the interaction. By default, changes are captured when they’re within the view hierarchy being animated as part of the transition.

So the above code works for navigation stack pops; the table view — or one of its ancestors — is being animated in order to bring it back on screen. The change in selection affects only descendant views of the table view, and so is captured as part of the transition.

But during a modal dismissal, the table view isn’t being animated; only the modal panel is. Instead, we need to set our code up a little differently to make the transition aware of it:

(sorry it’s rather wide; that’s animation code for you!)

It looks slightly more complex, but I’ve come to prefer this version. In particular, I like that line 7 makes it clear the deselection is to be animated as part of the transition. Handling cancellation is along much the same lines, but perhaps in a slightly more natural manner.

We explicitly pass the table as the view to be animated. The deselection happens within that hierarchy, so it's captured. If the transition is the result of popping a navigation stack, it still does the correct thing, too.

Now, how do we get Apple to actually implement this for us? :-)

© Mike Abdullah 2007-2015