- Make a new project, this time starting with an empty project, but otherwise with the same settings as before. Call it
Quiz.
- Note: These instructions are for XCode 4.
- File -> New -> File ... -> Objective-C Class. Name
SJSUFirstViewController, superclass UITableViewController
- Edit
SJSUAppDelegate.m. Add #import "SJSUFirstViewController.h". Below // Override point for customization after application launch., put
SJSUFirstViewController *first = [[SJSUFirstViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc]
initWithRootViewController:first];
self.window.rootViewController = navigation;
- Place
Quiz.plist into the Quiz directory of your project. Right-click on the project and select Add Files to "Quiz". Add the file. Click on it to explore what's inside.
- Add the following instance variable to
SJSUFirstViewController, right above the first method:
NSArray *questions;
- Add this code below
// Custom initialization in initWithStyle:
self.title = @"Quiz";
NSString *path = [[NSBundle mainBundle] pathForResource:@"Quiz" ofType:@"plist"];
questions = [NSArray arrayWithContentsOfFile:path];
- Change
numberOfSectionsInTableView to return 1 and tableView:numberOfRowsInSection to return [questions count]
- Under
// Configure the cell... in tableView:cellForRowAtIndexPath, put
cell.textLabel.text = [[questions objectAtIndex: indexPath.row] objectForKey: @"text"];
- Add this call to register the cell prototype in
viewDidLoad:
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"Cell"];
- Run the program. What happens?
- Click on a row. What happens? Why?