Handling cell actions with Swift generics

Igor Nemenonok
Nov 6, 2017 · 4 min read

Give iOS developer a table view and he will write 10+ implementations of how to handle cell actions.

As we are dealing with tables and collections quite often in Chili Labs, I wanted to make a generic solution how we are going to handle cell actions in our projects.

The solution with cell configurators that was described in my previous article works pretty well. Thus it could be a good idea to take it as a base pattern.

First of all, let’s add TableDirector class that will store cell configurators and will be a delegate and a data source for UITableView.


Let's make an enum for some base cell actions. It has to conform to Hashable protocol as its hash value will be used later.


Next, we need to create a class that will proxy cell actions and will store cell actions handlers. Right here we need CellAction’s hashValue as a part of a storage key.


We need to implement static property reuseIdentifier in ConfigurableCel as it's used as a part of a key for actions dictionary.

It’s time to invoke first cell actions in TableDirector class.

The initial implementation is done. Now we can add handlers for cell actions. TableViewController class can be edited as follows:

Subscription to cell actions takes place in addHandlers() method.

What are the benefits from handling cell actions like that?

  • All actions processing are in one place.
  • Configurator, data model and particular cell are available in closure.
  • No if statements or switches. You can add a handler for particular cell action.
  • Reusable solution.

Custom cell actions

First, we need to add one more case to CellAction enum and return its hashValue.

The most challenging thing here is how to pass custom action to the CellActionProxy object. It can be done by sending Notification via NotificationCenter. We can add an extension to CellAction enum for the convenient way of sending notifications. Action data will be stored in a struct and sent in the userInfo parameter.

Finally, we need to subscribe and process notification in TableDirector class.

Now we can invoke “Follow” action and handle it.

That’s it! Now we have a mechanism to handle any cell action generically. With tiny modifications it can be used to handle UICollectionViewCell actions.

Bonus: Animated change of data source

There is a good Swift port of IGListKit’s IGListDiff that will help us to find those changes in the data source.

First, we need to add the hash variable to CellConfigurator protocol.

protocol CellConfigurator {    var hash: Int { get }

Now we need to implement that hash variable in TableCellConfigurator class. Here we need to provide unique hash that depends on CellType and DataType. We can use hashValue of Hashable protocol, but to do that we need to add one more constraint to DataType.

class TableCellConfigurator<CellType: ConfigurableCell, DataType: Hashable>

And now we can add the implementation:

var hash: Int {     return String(describing: CellType.self).hashValue ^ item.hashValue}

Next, we need to conform User struct to Hashable protocol. String and URL already support Hashable protocol.

The last thing to do is to animate changes in the data source.

And voila, data changes with beautiful animation.

Hope this article will help you in your projects.

Complete source code can be found on Github (branch cellActions).

Thanks for reading!

Receive a new Mobile development related stories first. — Hit that follow button
Twitter: @ ChiliLabs

Chili Labs

Fascinating journey of a mobile development company from…