Igor Kulman

Simpler and safer iOS custom table view cells with Reusable

· Igor Kulman

When you create a UITableViewCell that you want to use with multiple UITableViews and design its view using a XIB file you know that registering and using it involves the usage of string constants. When you register your custom UITableViewCell with the UITableView you use a string as XIB name and a string as the reuse identifier. Then you use the string reuse identifier again to actually use it. There must be a better, safer way, right? Of course there is, just use Reusable.

Reusable is a Swift mixin for reusing views easily and in a type-safe way for UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers, Storyboards. It contains protocols you add to your classes and let the magic (the default implementation for those protocols) happen.

So how do you get rid of all those strings when using custom cells with UITableView? First, add the NibReusable protocol to you custom cell class

class UserCell: UITableViewCell, NibReusable

This assumes that your XIB for that class has the same name as the Swift class. The protocol adds to properties to your class, one for the XIB name and another one for the reuse identifier. The default implementation takes care of setting them both to the name of your class.

To register the UITableViewCell class you just use

tableView.register(cellType: UserCell.self)

and the actual usage is also very simple

let cell: UserCell = tableView.dequeueReusableCell(for: indexPath)

As promised, both registration and usage of the custom UITableViewCell can be done without the use any strings.

See also