Custom TableView Cell

Create the TableView

First, we create a tableView member variable in our ViewController

Second, we declare its property, and synthesize

Then in the viewDidLoad, we allocate for the tableView and add it to the view hierarchy.

Result:

emptyTable

We now have an empty table. However, it doesn’t do anything because we need to conform to the UITableViewDataSource to let it know what kind of data it should have and display.

Conforming to UITableViewDataSource

We need to modify the table to our liking by working with its data source delegate methods. We first make it our ViewController conform to the UITableViewDataSource delegate:

Make sure you assign the dataSource

After declaring the protocol, you’ll get warnings to implement the required methods:

Implement the required numberOfRowsInSection method. We say we want 8 rows

Implement the required cellForRowAtIndexPath method. We tell the delegate that we want to draw our cell like standard but with certain text:

Result:

textTable

Creating the Custom Cell

First, create MyCustomTableCell class:

MyCustomTableCell.h

MyCustomTableCell.m

In your UIViewController, let’s change the cellForRowAtIndexPath method so that it takes our custom cell:

Result:

customCell