iPhone Game Development - Chris Craft [118]
- (void)viewDidLoad {
[super viewDidLoad];
// The title of the main view controller will be used as the title of the
// navigation bar.
// This is actually a nice feature of the MVC architecture because if you
// decide to use a tab view instead it will use the same title.
self.title = @”Amuck-Tac-Toe Menu”;
UIImage *backgroundImage = [UIImage imageNamed:@”chalkGreen.png”];
self.view.backgroundColor = [[UIColor alloc]
initWithPatternImage:backgroundImage];
self.tableView.sectionHeaderHeight = 30.0f;
[backgroundImage release];
}
FIGURE 8.14
Select how to connect to other players in the Amuck-Tac-Toe main menu.
The method numberOfSectionsInTableView: returns the number of sections in a given table view. In our table view we have two. The first is Connection Types and the second is About and Options:
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
Next, we need to implement the method tableView:numberOfRowsInSection. In this method we inform the UITableView how many rows to render per section.
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 4;
}
else {
return 2;
}
}
The method tableView:cellForRowAtIndexPath: (Listing 8.1) is more involved, but you will get the hang of it soon enough. In this method you must return a properly initialized cell view for every row that is requested. You are given the section and row that are requested in the form of an NSIndexPath. Also notice that the cell views are being cached. This is very beneficial for performance. Constructing an object for every row takes time and consumes resources. Be sure to use the cache available to speed up your views.
Cross-Reference
To download Listing 8.1, go to www.wileydevreference.com and click the Downloads link.
Listing 8.1
Supplying Cell Data When the tableView:cellForRowAtIndexPath Method Is Called
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @”Cell”;
static NSString *SubtitleCellIdentifier = @”SubtitleCell”;
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *subtitleCell = [tableView
dequeueReusableCellWithIdentifier:SubtitleCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
autorelease];
}
if (subtitleCell == nil) {
subtitleCell = [[[UITableViewCell alloc]
continued
Listing 8.1 (continued)
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SubtitleCellIdentifier] autorelease];
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @”Head-to-Head”;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
case 1:
cell.textLabel.text = @”Nearby”;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
case 2:
cell.textLabel.text = @”New Internet”;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
default:
if (self.activeGameCount > 0) {
subtitleCell.textLabel.text = @”Continue”;
NSString *gameText =
self.activeGameCount > 1 ? @”games” : @”game”;
subtitleCell.detailTextLabel.text = [NSString
stringWithFormat:@”%d %@ currently active”,
self.activeGameCount, gameText];
subtitleCell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;
return subtitleCell;
}
cell.textLabel.text = @”Continue”;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
else {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @”Internet Settings”;
cell.detailTextLabel.text = @”Change your connection options”;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;