Register & Maintain the UITableViewCell in an easy way

Kerim Njuhovic
2 min readNov 14, 2018

--

One liner code is not good in the most of the cases, I agree, but for those who likes it, here is one of them. So, the old school way would require from you firstly to create UINib object and to load it, afterwards registering it in table view. In short

let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)
tableView.register(register: nib, forCellReuseIdentifier: "yourIdentifier")

Now, in case you have multiple types of cells, it can easily grow up. What you think about this:

No more strings manipulation and remembering name of nib, nor cell reuse identifier. Additionaly, in case you add any new cells they get atomatically registered. So, let’s go…

You will need to create simple protocol which will conform itself to CaseIterable (it is type which provides a collection of all of its values, more info about it), which gives us easily accessible allValues. Our protcol should contain simple property of type AnyClass (represeting our UITableViewCell or CollectionViewCell). All in all

So, let us consider you have home screen with multiple cell types to be loaded. Let’s create enumeration conforming to protocol created previously.

Now, final step is to extend functionalities of UITableView by creating the extension. The old school way with a bit of modifications 😊

Not that much complicated as it may seem. I hope i will get those One-Liner haters 😀. If you like it, don’t forget to give small applause, will appreciate it. Happy coding

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kerim Njuhovic
Kerim Njuhovic

Written by Kerim Njuhovic

iOS & tvOS developer, 🚴‍♂️ & 🏔 lover

No responses yet

Write a response