I am trying out iPhone Apps, coming from C#, I am finding very difficult to grasp the iPhone dev environment.
So far, I am able to get my 'Makes' listed in a TableViewController. I now wish to get by 'Models' listed when a Makes is selected. Can someone help me with sample code.
Can I use the same TableViewController to show the Models of the cars?
I did not create any Nibs, I am doing all in code.
The Sql for the details would be: select title from make where parentkey = ? The parentkey is the primarykey of the make.
// MakeListTableViewController.h
#import <UIKit/UIKit.h>
#import "ModelTableViewController.h"
@interface MakeListTableViewController : UITableViewController {
NSMutableArray *makes;
ModelTableViewController *modelTableViewController;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) ModelTableViewController * modelTableViewController;
@property (nonatomic, retain) UITabBarController *tabBarController;
@end
// MakeListTableViewController.m
#import "MakeListTableViewController.h"
#import "ModelTableViewController.h"
#import "sqlite3.h"
static int MyCallback(void *context, int count, char **values, char **columns)
{
NSMutableArray *categories = (NSMutableArray *)context;
for (int i=0; i < count; i++) {
const char *titleCString = values[i];
[makes addObject:[NSString stringWithUTF8String:titleCString]];
}
return SQLITE_OK;
}
@implementation MakeListTableViewController
- (void)loadNamesFromDatabase
{
NSString *file = [[NSBundle mainBundle] pathForResource:@"CarList" ofType:@"sqlite"];
sqlite3 *database = NULL;
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
sqlite3_exec(database, "select title from make where parentkey = 0", MyCallback, makes, NULL);
}
sqlite3_close(database);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return 0;
return [makes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// THIS IS WHERE I THINK THE INDEX IS PASSED ON FOR THE DETAILS
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [makes objectAtIndex:indexPath.row];
//return cell;
return [cell autorelease];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
makes = [[NSMutableArray alloc] init];
[self loadNamesFromDatabase];
}
return self;
}
- (void)dealloc {
[makes release];
[super dealloc];
}
@end
SQL
CREATE TABLE make(pk INTEGER PRIMARY KEY, parentkey INTEGER DEFAULT 0, title TEXT);
INSERT INTO make(title) VALUES('Honda');
INSERT INTO make(title) VALUES('Toyota');
INSERT INTO make(title) VALUES('Mazda');
INSERT INTO make(title) VALUES('Nissan');
INSERT INTO make(title, parentkey) VALUES('Civic', 1);
INSERT INTO make(title, parentkey) VALUES('Accord', 1);
INSERT INTO make(title, parentkey) VALUES('Corolla', 2);
In my C# programs I am used to using the primary key to get the details record, like the GridView, is this how it is also done in the iPhone?