Sunday, May 8, 2011

Table in iPhone sdk

Hello Friends,

Today we will learn Table implementation methods in iOS SDK. Table is very important part of the iPhone development. 90% of the applications are made with use of table. Table view in iPhone SDK provides very flexibility in designing and coding. All the contents can be scrollable with table view. Contact application in iPhone is built with the use of table view only. Table view implementation is very easy and its data source and delegate methods enhance its flexibility. So, now here we will learn some data source and delegate methods for the table view.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}


The main method of Data Source is to fill the data in the table. Above methods create table and following methods will fill the data in table.

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}


To set the actions when tables rows are pressed we have to create Delegate methods for that. Delegate methods will create an action when user will tap the Cell or Row of the table. We will learn one main method of delegate here. Following method creates action for our table.

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

// Generally here we put some action code like when user taps cell, new view or nib is pushed from the current nib.

Detailview *detailViewController = [[Detailview alloc] initWithNibName:@"Detailview" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

}


Above code will push new xib for the detail view. Alert view is also implemented in this method.

No comments:

Post a Comment