How To Use TableView In Iphone?

Let’s look at the simplest possible example of a table view to get a feel for how its works. In this example ,we are going to display a list of  text value.

Step 1: Create a View-based application project in Xcode.

Step 2: Double click .xib file to open the file in Interface Builder. Just look in the libarary for aTable View and drag that over to the View window. After dropping the table view onto the view window,single click it to select it,and bring up the connections inspector. You’ll  notice that the two available connections are there dataSource and delegate. Drag from the circle next to each of those connections over to the File’s Owner icon.After doing that ,save,close and go back to xcode.

Figure 1: The View window after the table view is placed

step 3: All we’re doing here is conforming our class to the two protocols that are needed for it to act as the delegate and datasource for the table view.


#import <uikit/UIKit.h>

@interface TableViewViewController : UIViewController
<uitableViewDelegate,UITableViewDataSource> {
NSArray *listData;



Step 4: In the viewDidLoad,we’re simply creating an array of data to pass to the table.


- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy",@"Sneezy",@"Bashful",@"Happy",@"Doc",
@"Gloin",@"Bifur",@"Bofur",@"Bombur",nil ];

self.listData = array;
[array release];
[super viewDidLoad];



Step 5: We are added here two datasource methods. The first method, tableView:numberOfRowsInSection:,is used by the table to ask how many rows are in a particular section.And this method will be called to get the number of rows in the one section that makes up the list. And the next method, is called by table view when it needs to draw one of its rows.The second argument to this method is an NSIndexPath instance. This is the mechanism that table views use to wrap the section and row into a single object.


-(NSInteger)tableView:(UITableView *)tableView
return [self.listData count];

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier] autorelease];
NSUInteger row = [indexPath row];
cell.text = [listData objectAtIndex:row];
return cell;




Figure 2: Each row of the table is drawn with an indent level higher than the row before it.

Step 6: Now,we’ll just throw up an alert to show that the row was selected. Add the following method in your code.


-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
@"You selected %@",rowValue];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:
@"Row Selected!"
cancelButtonTitle:@"Yes, I did."

[alert show];
[message release];
[alert release];



Figure 3: Alert is display when row is selected.

You can downloaded SourceCode from here TableView

Leave a Comment:

Blah says July 7, 2010

Is there something missing? I followed whole but doesn’t work?

Sushant says July 7, 2010

I have uploaded SourceCode once again. Now you can check it. Hope it will help you. 🙂

SmartiOS says October 28, 2012

Alert is display when row is selected. doesn’t work??

Add Your Reply