views:

722

answers:

1

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?

+1  A: 

A couple of things going on here...

1- You are not selecting the primary key back from your sql query. You need to change it to something like "SELECT pk, title...." in order to retrieve that.

2- You are only storing the title back in your makes. Ideally you want to create some data structure (probably a class) which you store in your array so you have access to the PK later on.

3- You need to implement the didSelectRowAtIndexPath method on the UITableView to determine which row was touched in the tableview.

Once you have these things worked out... What you want to do is push a configured (ie: you've told it which make) ModelTableViewController onto your navigation controller.

You should probably take a look at the Elements sample on the developer site. It's a very good example of using SQLite to populate a UITableView.

Lounges