You see UITableView
in all sorts of utility iPhone applications. It is used to display data, lists of items and even to format objects on the touch display. UITableView
is one of the things that you can learn that will bring you a ton of leverage in your iPhone app development projects. Today, I am going to show you the basics (scroll down to see the video) of using UITableView
.
UITableView in iPhone & iPad Programming
UITableView
uses the delegation pattern that is very common through iPhone SDK. Generally, the idea is that you will need an object to act of behalf of your UITableView
; we will call this object a delegate
. When the system needs to know something, like how many rows are in the table, it will simply “ask” the delegate
using a specified callback or delegate
method.
Delegation is a really useful skill to master and it is used throughout iPhone development. If you need help with delegation and other iPhone SDK design patterns, check out these resources that will help you. You may also want to check out the tutorial section on this website.
iPhone SDK 3.2 Must Be Installed
You will need to have iPhone SDK 3.2 installed. Also, before we start, make sure to have created a window-based iPhone application.
Step By Step
- Create Window-based iPhone application
- Add New
UITableViewController
Subclass
- Implement
numberOfSectionsInTableView
- Implement
numberOfRowsInSection
cellForRowAtIndexPath
- Import
UITableViewController
Subclass
into your app delegate - Create an instance of your
UITableViewController
Subclass
- Add your
UITableViewController Subclass
object to the UIWindow
- Build and Go
Building UITableView Video
Having trouble seeing the video? Click here to download the mp4 file to your desktop to see the video in your favorite player.
UITableView Source Code
#import "TableViewOne.h"
@implementation TableViewOne
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 5;
}
// 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] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];
return cell;
}
@end
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.