Iphone Tutorials UITable view

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 VIEWBASED APP named simple table
2.Modify code in the SimpleTableViewController.h file as follow:

1 #import

3 @interface SimpleTableViewController : UIViewController
4 {
5 NSArray *listData;
6 }
7 @property(nonatomic, retain) NSArray *listData;
8 @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:

01 #import "SimpleTableViewController.h"

03 @implementation SimpleTableViewController
04 @synthesize listData;
06 - (void)viewDidLoad {
07 NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod",
08 @"iPad",nil];
09 self.listData = array;
10 [array release];
11 [super viewDidLoad];
13 }
14 - (void)dealloc {
15 [listData dealloc];
16 [super dealloc];
17 }
19 #pragma mark -
20 #pragma mark Table View Data Source Methods
22 - (NSInteger)tableView:(UITableView *)tableView
23 numberOfRowsInSection:(NSInteger)section
24 {
25 return [self.listData count];
26 }
28 - (UITableViewCell *)tableView:(UITableView *)tableView
29 cellForRowAtIndexPath:(NSIndexPath *)indexPath
30 {
32 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
33 UITableViewCell *cell = [tableView
34 dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
35 if (cell == nil) {
36 cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
37 reuseIdentifier:SimpleTableIdentifier] autorelease];
38 }
40 NSUInteger row = [indexPath row];
41 cell.textLabel.text = [listData objectAtIndex:row];
42 return cell;
44 }
45 @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.
1 (NSInteger)tableView:(UITableView *)tableView

2 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
1 - (UITableViewCell *)tableView:(UITableView *)tableView

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

1 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 cinterviews.com here

Output will look like this:

No comments:

Top Blogs