Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 3.74 KB

README.md

File metadata and controls

35 lines (25 loc) · 3.74 KB

GenricTables

How To Create Generic Table And Its Cells Using UI-Kit

UITableViews and UICollectionViews are the most commonly used interface components on IOS. They can be found on almost any app. In this tutorial, we will only be focusing on UItable Views. Luckily, implementing UITableView is not that difficulty. Below is the minimal code we need to implement our UITable using storyboard.

1

But if an app needs another Table. Normally we would start by creating another view controller follows the above steps and would look like this.

2

Problem with above approach is, we will have the same code again and again for every TableView. Well,in this tutorial we will learn how to solve this issue with help of generics. We will start with renaming our VIewController class to BaseTableViewController and will pass a Generic T of Type UITableViewCell. Now our base class will look like this.

3

3a

To run our app, all we need to do is create a VIewController and make it inherit from BaseTableViewController. Finally populate the list with string

4

You can see, we manage to remove all that repeatable code. But we have problem with list, it can only have strings as a value. But if one of the tables needs to show objects, Int or any other type. So, to solve This issue, we will introduce another Generic U and now our base class will look like this

5

And our VIewController Class will be

6

Now we can pass any type to our list. But now we have another problem. Every time, we inherit from base class, we will have to override CellForRowAt method. Let’s solve This problem by introducing another Generic. This time we will create base class for Cell, and it will look like this.

7

Now instead of passing UITableViewCell, we will pass BaseCell to our BaseTableViewController. Now our base table view class will look like this.

8

To run our App, all we need is cell which inherits from BaseCell and a VIewController which inherits from BaseTableViewController.

Below are two examples, where our VIewController is rendering list of type Int and String Without overriding Any parent class Methods

9

10

That’s it. Once you have your base cell and Base table class setup Properly. All you need is the above snippet code to display data on the UITableView.

Thank You.