Posted on by & filed under Content - Highlights and Reviews, Mobile Development, Programming & Development.

UITableView is one of the most used UI elements in iOS apps. This post will cover a very simple example that explains how to create a UITableViewController and populate with the data in an array.

First, create a new XCode project. Select Single View Application. Make sure you check Use Storyboards. If you need more information about creating an iOS app, read this section in Programming in Objective-C, Fifth Edition by Stephen G. Kochan.

Add a new file and select Objective-C class. In the Subclass of textfield, select UITableViewController. Name the class as MyTableViewController.

In storyboard, drag-drop TableView Controller from the objects library to create a new Table View in storyboard.

Then select your new TableView from the storyboard and under identity inspector, change the class to MyTableViewController. Now this TableView is hooked up with the MyTableViewController class.

In the storyboard, inside the TableViewController, click the blank space under the Prototype Cells.

Now go to the attributes inspector and in style select basic and in identifier write Cell.

Now write the code to populate the data in TableViewController. There are 3 main delegate methods that we are going to use:

Before everything else, we will first create an array that will have NSString values of different colors. The values of the array will be populated in the UITableView.

Create a new property in header file (.h) as follows:

Make sure you have synthesized this property in the implementation (.m) file.

Now, in the implementation (.m) file, inside viewDidLoad, initialize the array and add few NSString objects. Use the code below:

Next, we will write the code in delegate methods. As we have only one section in the TableView, write the following code in numberOfSectionsInTableView:

The numberOfRowsInSection methods returns the number of rows that the TableView will have. In our case, the number of rows in the TableView should be the count of objects in the array.

At last, we will write the following code in the cellForRowAtIndexPath method. This will add the values of NSArray to the cells of TableView:

In the code above, cell.textLabel is the label inside the cells of TableView. Whereas indexPath.row returns the index number of the cell that the delegate is iterating right now.

Now go to storyboard and in the first UIViewController, insert a UIButton and label it “Go To Table”. Hold the ctrl key and click on the button and drag it to the UITableView Controller’s UIView. When the Action Segue menu opens, click modal to set a segue from this button to the UITableView’s UIView. Your storyboard should look like this:


We are done here. Run the app and click the Go To Table button and it should work out-of-the-box.

Safari Books Online has the content you need

Check out these Objective-C books available from Safari Books Online:

Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apple’s iOS and OS X platforms. The book makes no assumptions about prior experience with object-oriented programming languages or with the C language (which Objective-C is based upon). Because of this, both beginners and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C. Readers can also learn the concepts of object-oriented programming without having to first learn all of the intricacies of the underlying C programming language.
As iOS apps become increasingly complex and business-critical, iOS developers must ensure consistently superior code quality. This means adopting best practices for creating and testing iOS apps. Test-Driven Development (TDD) is one of the most powerful of these best practices. Test-Driven iOS Development is the first book 100% focused on helping you successfully implement TDD and unit testing in an iOS environment.
Foundation iPhone App Development: Build An iPhone App in 5 Days with iOS 6 SDK quickly enables existing programmers to become familiar and comfortable coding Objective-C using Xcode 4.5, Storyboarding and the iOS 6 SDK to create apps for the iPhone. Nick Kuh, an experienced, Apple award-winning developer, will teach readers how to build an iOS 6 iPhone app from start to finish in 5 days.
In these video LiveLessons, Jiva Devoe helps the viewer understand the key concepts and fundamentals of the Objective-C language. Objective-C Programming Fundamentals LiveLessons (Video Training) covers basic objective-c syntax, objects, memory management, protocols, the foundation framework and errors. The newest features in Objective-C such as ARC, blocks, and properties are included. After watching this video, viewers should be able to build and run basic Objective-c programs.

About the author

Umair Khan Jadoon is an avid developer. He enjoys building awesome apps for mobile and web and occasionally writing about technology. Geek at heart, he’s working on establishing his own mobile app startup.

Tags: iOS, Objective-C, UITableView, UITableViewController,

One Response to “Objective-C: How to Populate Data in UITableViewController”