How to create UITableView programmatically in Objective-C

Usually when you need to implement tableview in your app you select a master-detail application:

new Xcode project Master detail

Were you get the project all the UITableView implementation ready for you to start adapting in your application,but sometimes you need just a tableView either to display the configuration of your app or to display some other kind of information.

The implementation of the tableview is very simple to do. You need to implement the following methods:

This method is optional if you have only one section in your tableview:

Lets start implementing our tableview.

– Open Xcode and go to the file menu and select new and select new
project or press Ctrl+Shift+N.
– Select single view application and click next.
– Select the name of your project.
– Select the location where you want to save your project.

new Xcode project single view application

Go to viewController implementation file (viewController.m).

In the interface line you need to add the following delegates:


It will look like this:

Bellow the @interface line we need to add two properties a tableview property and NSArray property for our content in the tableView:

In your viewDidLoad method we are going to add few lines of code. First we need to create our content using the NSArray. We are going to use the days of the week for this example.

Now that we have our content we need to initialize our tableview and set the delegate and data source for our tableView. First we are going to initialize our tableView Property. We are going to initialize the frame of the tableview with the bounds of our view and we are going to select the style UITableViewStylePlain

We have initialized our tableView property now we need to assign the delegate and data source:

and the last line of code in your viewDidLoad will be to add the tableView to the main view:

Now we are going to implement the number of sections in the table view:

As I mentioned before this method is optional . By default the number of sections is 1.

In numberOfRowsInSection we are going to use the count of elements in our NSArray to tell the tableView how many rows we are going to be using:

time to populate our tableView. We need to implement the following method:

Inside of this method we need to create a static property to identify the cells in our tableView and initialize UITableViewCell:

Now we need to add some safety checking in our cells in case they are null:

The last part of the implementation in this method is to actually assign the values of our NSArray to the cells:

The last method we are going to implement is didSelectRowAtIndexPath to register when the user taps in a cell. At this point we are only going to log when the user has tap in the cell:

Your implementation should look like this:

If you click on the run in your Xcode project should look like this:

Xcode project with tableview Objective-C

Please let me know if you have any questions by leaving a comment below or on twitter @luisemedr