We can get those awesome "swipe-to-reveal" actions from Mail.app in our own table views. We just need to implement one delegate function and return an array of UITableViewRowActions.
overridefunctableView(tableView:UITableView,editActionsForRowAtIndexPathindexPath:NSIndexPath)->[UITableViewRowAction]?{letdeployAction=UITableViewRowAction(style:.Default,title:"Deploy"){(action,indexPath)in// TODO: Deploy the troop at this indexPath.row}return[deployAction]}
Adjusting to State Transitions
We can override the willTransitionToState(state:) and didTransitionToState(state:)functions in our UITableViewCellsubclasses to run code when the cell begins or finishes showing the edit control or delete confirmation:
Note how we not only give the cell a detail button, but we pass in a closure to be run when the detail button is tapped. We can even use a custom UIView as an accessory like we did with the switch in our first example.
We used a simple string as our footer here, but we can easily use a view instead:
Section(rows:[],footer:.View(footerView)).
Static does a great job separating concerns. Rows don't know anything about the actual UITableViewCells that eventually get shown to represent them. Static uses the .Value1 cell type by default and comes with a few other standard cell types. We can easily add our own by implementing the CellType protocol on any of our own UITableViewCell subclasses.
Let's say you have a table view with some cells showing a list of crew members. It'd be great if when the user taps a cell, it would “expand” to reveal some actions that can be performed on that crew member. Let's dive in.
Setup View Hierarchy
As you can see, we lean on UIStackView pretty heavily. The constraint shown here is the one we'll be animating.
Animate the Constraint
In our cell's setSelected(animated:) function, we animate the constraint just like we covered back in Bite #9. After the animation's done, we hide/show the toolbar. This triggers the top-level stack view to recalculate it's height.
Our view controller takes care of resizing our cells by updating the 'expanded' state of each crew member when the selection changes and a pair of calls to tableView.beginUpdates/endUpdates inside the tableView:didSelectRowAtIndexPath: and tableView:didDeselectRowAtIndexPath:.
Download the complete working project here: j.mp/bite050