Handling cell actions with Swift generics

Igor Nemenonok
Nov 6, 2017 · 4 min read
Image for post
Image for post

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

As we are dealing with tables and collections quite often in , 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 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 .

TableDirector.swift

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

CellAction.swift

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

CellActionProxy.swift

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

The solution doesn’t make sense if we can’t process custom cell actions. For example, you need to add “Follow” button as a part of user cell. So, now we must handle two actions for one cell, and one of them is triggered by a button within the cell.

Image for post
Image for post

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 via . 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

Usually, data in the app is not static and can be changed over the time (e.g., after an update from the server). Reloading a table view after each data source change is not a good idea. UITableView has a built-in API for data source change animation, and it works really good. When data source changes we need to find how many rows were added, deleted, moved or reloaded. But how to do that?

There is a good Swift port of ’s 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 of 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 protocol. and already support protocol.

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

And voila, data changes with beautiful animation.

Image for post
Image for post

Hope this article will help you in your projects.

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

Thanks for reading!


Image for post
Image for post

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

Chili Labs

Fascinating journey of a mobile development company from…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store