如何使用Sqlite在iPhone应用程序中显示详细信息视图
问题描述:
我正在尝试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 - 你没有选择的主键从您的SQL查询回来。您需要将其更改为“SELECT pk,title ....”以便检索该内容。
2-您只将标题存回您的作品中。理想情况下,您希望创建一些数据结构(可能是一个类),并将其存储在数组中,以便稍后访问PK。
3-您需要在UITableView上实现didSelectRowAtIndexPath方法,以确定在tableview中触摸了哪一行。
一旦你得到了这些东西......你想要做的是将一个已配置的(即:你已经告诉它是哪个)ModelTableViewController推到你的导航控制器上。
您应该看看开发人员网站上的Elements示例。这是使用SQLite填充UITableView的一个很好的例子。