In this UITableView tutorial I teach you how to push a new UITableView or UIView. The extra teaches you how to load different data into the table view from different arrays depending on what row is clicked.
Main Code Used:
In RootViewController.m:
#import "PetsTableViewController.h"
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PetsTableViewController *pets = [[PetsTableViewController alloc] initWithNibName:@"PetsTableViewController" bundle:nil];
if ([[petsArray objectAtIndex:indexPath.row] isEqual:@"Dog"]) {
pets.petsInt = 0;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if ([[petsArray objectAtIndex:indexPath.row] isEqual:@"Cat"]) {
pets.petsInt = 1;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if ([[petsArray objectAtIndex:indexPath.row] isEqual:@"Snake"]) {
pets.petsInt = 2;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
[self.navigationController pushViewController:pets animated:YES];
[pets release];
}
In PetsTableViewController.h:
NSMutableArray *dogArray;
NSMutableArray *catArray;
NSMutableArray *snakeArray;
int petsInt;
}
@property int petsInt;
In PetsTableViewController.m:
In viewDidLoad:
dogArray = [[NSMutableArray alloc]
initWithObjects:@"Dog 1", @"Dog 2", @"Dog 3", @"Dog 4", nil];
catArray = [[NSMutableArray alloc]
initWithObjects:@"Cat 1", @"Cat 2", nil];
snakeArray = [[NSMutableArray alloc]
initWithObjects:@"Snake 1", @"Snake 2", @"Snake 3", nil];
Don't forget to release them:
(void)dealloc
{
[dogArray release];
[catArray release];
[snakeArray release];
[super dealloc];
}
In numberOfSectionsInTableView return 1;
In numberOfRowsInSection:
if (petsInt == 0)
return [dogArray count];
if (petsInt == 1)
return [catArray count];
if (petsInt == 2)
return [snakeArray count];
[self.tableView reloadData];
In cellForRowAtIndexPath:
if (petsInt == 0)
cell.textLabel.text = [[dogArray objectAtIndex:indexPath.row] retain];
if (petsInt == 1)
cell.textLabel.text = [[catArray objectAtIndex:indexPath.row] retain];
if (petsInt == 2)
cell.textLabel.text = [[snakeArray objectAtIndex:indexPath.row] retain];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
Next Video: • UITableViews: Pushing a detail view using ...
UITableView Playlist: http://www.youtube.com/playlist?p=PLD...
Previous Tutorial: • iPhone Programming-UITableViews: Setting u...
Twitter: / failcakeapps
Apple Developer Center: http://developer.apple.com/devcenter/...
Website: http://failcake.webs.com/
Channel: / milmersxcode