Table View Application


 

Table View Application

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 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

0

{

[super viewDidLoad];

 

1

//Initialize the array.

2

 

listOfItems = [[NSMutableArray alloc] init];

 

3

//Add items

4

[listOfItems addObject:@"India"];

[listOfItems addObject:@"Greenland"];

[listOfItems addObject:@"Switzerland"];

5

[listOfItems addObject:@"Norway"];

[listOfItems addObject:@"New Zealand"];

[listOfItems addObject:@"Greece"];

6

[listOfItems addObject:@"Italy"];

[listOfItems addObject:@"Ireland"];

//Set the title

7

self.navigationItem.title = @"Countries";

}

8

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.

9

 

 

0

Selecting number of Sections In Table View:

pragma mark Table view methods

1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

2

}

add no of rows:

3

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

{

4

return [listOfItems count];

}

5

Customize the appearance of Table View cell:

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

6

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

7

if (cell == nil)

{

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

8

}

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

cell.text = cellValue;

9

return cell;

}

0

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

{

1

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

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

2

// [self.navigationController pushViewController:anotherViewController];

3

// [anotherViewController release];

}

Finally RootViewController.m file will look like this:

4

#import "RootViewController.h"

#import "TableView1AppDelegate.h"

5

@implementation RootViewController

 

6

- (void)viewDidLoad

{

7

[super viewDidLoad];

 

8

//Initialize the array.

 

9

listOfItems = [[NSMutableArray alloc] init];

 

0

//Add items

 

1

[listOfItems addObject:@"India"];

[listOfItems addObject:@"Greenland"];

[listOfItems addObject:@"Switzerland"];

2

[listOfItems addObject:@"Norway"];

[listOfItems addObject:@"New Zealand"];

[listOfItems addObject:@"Greece"];

3

[listOfItems addObject:@"Italy"];

[listOfItems addObject:@"Ireland"];

 

4

//Set the title

5

self.navigationItem.title = @"Countries";

}

 

6

- (void)didReceiveMemoryWarning

{

7

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

[super didReceiveMemoryWarning];

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

8

}

 

9

- (void)viewDidUnload

{

}

0

 

#pragma mark Table view methods

1

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

2

{

return 1;

}

3

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

 

4

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

{

return [listOfItems count];

5

}

// Customize the appearance of table view cells.

 

6

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

{

7

static NSString *CellIdentifier = @"Cell";

 

8

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)

{

9

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

}

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

0

cell.text = cellValue;

 

1

return cell;

}

 

2

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

{

3

 

}

4

 

- (void)dealloc

5

{

[listOfItems release];

[super dealloc];

6

}

 

7

@endx

 

8

Press Build And Go

Download code from here.

 

9

Ads