UITableView Hello World
May 24th, 2010 § Leave a Comment
Usually writing a tutorial blog takes a lot of time and effort to document every step and explain all details. I have seen many HelloWorld app that show very little on how iPhone app and SDK work. I include the best HelloWorld tutorial and example I found.
These are the most important building blocks of iPhone App. However usually developers don’t start with new app and may be working on existing app. Sometimes developers never get to build new View Controller and/or view. You won’t remember all the details in each step. It’s nice to reference this kind of Hello World example.
It’s nice to see how a navigation-based app with minimal functionality and show how the navigation bar and button work.
UITableView Hello World
http://icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/
Add View Controller and View and Back Button to HelloWorld
http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/
Note: Remember setText is deprecated in OS 3.2 and warning will appear in this Hello World example.
Populating UITableView with NSArray
http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/
I am including code snippet to show how to gain access to UIApplication delegate to return the NSArray object in UITable row.
In RootViewController.m
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Gain access to Application delegate
HelloNavigationAppDelegate *appDelegate = (HelloNavigationAppDelegate *)[[UIApplication sharedApplication] delegate];
// Return no of rows based on no. of objects in array
return appDelegate.fruits.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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Gain access to Application delegate
HelloNavigationAppDelegate *appDelegate = (HelloNavigationAppDelegate *)[[UIApplication sharedApplication] delegate];
Fruit *f = (Fruit *)[appDelegate.fruits objectAtIndex:indexPath.row];
cell.textLabel.text = f.name;
// Configure the cell.
return cell;
}
I have rewritten the UITableView Hello World sample in iPhone SDK 3.2 and replaced the deprecated API that no longer support UITableView. You can download the latest version here