Home Tutorial Iphone Examples Table View Application

 
 

Share on Google+Share on Google+
Table View Application
Posted on: August 25, 2009 at 12:00 AM
Advertisement
Table views are commonly found in iPhone applications, especially productivity applications. They are versatile user-interface objects that you can adapt for different needs.

Table View Application

Introduction:

Table views are commonly found in iPhone applications, especially productivity applications. They are versatile user-interface objects that you can adapt for different needs. A table presents a scrollable list of items (or rows) that may be divided into sections. Each row may display strings, images, or other identifiers of the represented data item.

Table View Programming Guide for iPhone OS explains the concepts underlying table-view programming and shows how to create and manage table views in your own projects. It starts by giving an overview of table styles and characteristics, as well as the programmatic interfaces related to table views. It also explains in general terms how to map collections of data at various levels of a hierarchical data model to sequences of table views, and how to use certain UIKit classes to support this traversal of a data hierarchy. After giving the conceptual background, the guide discusses various aspects of table view programming, such as creating and configuring table views.

Example:

In this application we are going to create Table View by using Navigation based application.

There is two .h and two .m file i.e. (RootViewController.h, RootViewController.m ,TableView1AppDelegate.h, TableView1AppDelegate.m) and one RootViewController.xib, one MainWindow.xib.

Navigation Based Application is chosen to display the title on Table View

We are going to add Table View to RootViewController.

RootViewController.h file will look like this:

@interface RootViewController : UITableViewController

{

NSMutableArray *listOfItems;

}

 

@end

In this we are creating an array to insert the value into Table View.

in .m file add:

 

- (void)viewDidLoad

{

[super viewDidLoad];

 

//Initialize the array.

 

listOfItems = [[NSMutableArray alloc] init];

 

//Add items

[listOfItems addObject:@"India"];

[listOfItems addObject:@"Greenland"];

[listOfItems addObject:@"Switzerland"];

[listOfItems addObject:@"Norway"];

[listOfItems addObject:@"New Zealand"];

[listOfItems addObject:@"Greece"];

[listOfItems addObject:@"Italy"];

[listOfItems addObject:@"Ireland"];

//Set the title

self.navigationItem.title = @"Countries";

}

Using array , adding object into the Table View and setting the title as Countries.

Make appropriate connection in interface builder after declaring the outlets in .h file.

 

 

Selecting number of Sections In Table View:

pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

add no of rows:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [listOfItems count];

}

Customize the appearance of Table View cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];

cell.text = cellValue;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

// Navigation logic may go here. Create and push another view controller.

// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];

// [self.navigationController pushViewController:anotherViewController];

// [anotherViewController release];

}

Finally RootViewController.m file will look like this:

#import "RootViewController.h"

#import "TableView1AppDelegate.h"

@implementation RootViewController

 

- (void)viewDidLoad

{

[super viewDidLoad];

 

//Initialize the array.

 

listOfItems = [[NSMutableArray alloc] init];

 

//Add items

 

[listOfItems addObject:@"India"];

[listOfItems addObject:@"Greenland"];

[listOfItems addObject:@"Switzerland"];

[listOfItems addObject:@"Norway"];

[listOfItems addObject:@"New Zealand"];

[listOfItems addObject:@"Greece"];

[listOfItems addObject:@"Italy"];

[listOfItems addObject:@"Ireland"];

 

//Set the title

self.navigationItem.title = @"Countries";

}

 

- (void)didReceiveMemoryWarning

{

// Releases the view if it doesn't have a superview.

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

 

- (void)viewDidUnload

{

}

 

#pragma mark Table view methods

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

// Customize the number of rows in the table view.

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [listOfItems count];

}

// Customize the appearance of table view cells.

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];

cell.text = cellValue;

 

return cell;

}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

 

}

 

- (void)dealloc

{

[listOfItems release];

[super dealloc];

}

 

@endx

 

Press Build And Go

Download code from here.

 

Advertisement

Related Tags for Table View Application:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 25, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com