UITableView Tutorial : Introduction to iPhone TableView
Table view is commonly used to show lists of data. A table view is the view object that displays table’s data and instance of UITableView. Each visible row in a table view is implemented by UITableViewCell. Table views are not responsible for storing your table’s data. They store only enough data to draw the rows that are currently visible. Table views get their configuration data from an object that conforms to the UITableViewDelegate protocol and their row data from an object that conforms to the UITableViewDataSource protocol.
Follow the steps below to create UITableView sample:
1.Open Xcode and create new view based project named ‘SimpleTable’
2.Modify code in the SimpleTableViewController.h file as follow:
#import <UIKit/UIKit.h>
@interface SimpleTableViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
NSArray *listData;
}
@property(nonatomic, retain) NSArray *listData;
@end
3.Open SimpleTableViewController.xib file and drag Table View from Library over to the View Window.
4.Connect tableview’s dataSource and delegate from connection inspector to File’s Owner.
5.Modify code in the SimpleTableViewController.m file as follow:
#import "SimpleTableViewController.h"
@implementation SimpleTableViewController
@synthesize listData;
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod",
@"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)dealloc {
[listData dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
@end
In viewDidLoad we are creating an array of data to pass to our Table View.
We have also added two more methods of data source delegate which are mandatory to implement when your implementing UITableViewDataSource delegate.
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
which specifies how many number of rows are there in one section of the Table View.
The default number of section in Table View is one.
Another method is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Which is called by table view when it needs to draw a row. This method is called n times and value of n is equal to value returned by first method. As this method is called once for every row,
if (cell == nil)
checks, if cell exits before, if not create new cell. Here ‘indexPath’ parameter gives us current Indexpath of the row from which we can get the current drawing row. Then we set the text of textLabel property of the current drawing cell and finally return the cell to the Table View.
You can download the source code used in this tutorial from here
Output will look like this:

about 5 months ago
I’m not quite understanding what’s happening on step four. What am I supposed to do?
about 5 months ago
I don’t understand what you mean by file’s owner, I’m really sorry if this is a dumb question, I’m pretty new to Objective-C.
about 3 months ago
RJ: hold down the control button and drag the Table View to the box that says “File’s Owner” on the top-left part of the Interface Builder screen. When the box pops up, select dataSource. Repeat this, and then select delegate.
about 2 months ago
How I can change the style of the cells?
Thanks!
about 2 months ago
how add buttons on cells
about 2 months ago
celltext style
about 5 days ago
i get a crash on launch in the main
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
Program received signal SIGABRT
I have connected the data source etc.. correctly i think.
about 5 days ago
disregard , i forgot to add @synthesize
thanks, great tutorial !