2009-06-15 61 views
1

我正在嘗試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做了什麼?

回答

1

幾件事情會在這裏......

1 - 你沒有選擇的主鍵從您的SQL查詢回來。您需要將其更改爲「SELECT pk,title ....」以便檢索該內容。

2-您只將標題存回您的作品中。理想情況下,您希望創建一些數據結構(可能是一個類),並將其存儲在數組中,以便稍後訪問PK。

3-您需要在UITableView上實現didSelectRowAtIndexPath方法,以確定在tableview中觸摸了哪一行。

一旦你得到了這些東西......你想要做的是將一個已配置的(即:你已經告訴它是哪個)ModelTableViewController推到你的導航控制器上。

您應該看看開發人員網站上的Elements示例。這是使用SQLite填充UITableView的一個很好的例子。

相關問題