我正在嘗試iPhone應用程序,來自C#,我發現很難掌握iPhone開發環境。如何使用Sqlite在iPhone應用程序中顯示詳細信息視圖
到目前爲止,我能夠將我的'使'列在TableViewController中。我現在希望在選擇完成後選擇'Models'。 有人可以幫助我的示例代碼。
我可以使用相同的TableViewController來顯示汽車的型號嗎?
我沒有創建任何Nibs,我正在做所有的代碼。
Sql的詳細信息是:select title from make where parentkey =? 父鍵是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);
在我的C#程序,我習慣使用主鍵來獲得細節記錄,像GridView的,這是怎麼它也是在iPhone做了什麼?