【问题标题】:How do I show details view in iPhone App using Sqlite如何使用 Sqlite 在 iPhone 应用程序中显示详细信息视图
【发布时间】:2009-06-15 07:07:22
【问题描述】:

我正在试用来自 C# 的 iPhone 应用程序,我发现很难掌握 iPhone 开发环境。

到目前为止,我能够在 TableViewController 中列出我的“Makes”。我现在希望在选择 Makes 时列出“模型”。 谁能帮我提供示例代码。

我可以使用同一个 TableViewController 来显示汽车的型号吗?

我没有创建任何 Nib,我都是在代码中完成的。

详细信息的 Sql 将是:select title from make where parentkey = ? 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中也是这样吗?

【问题讨论】:

    标签: iphone xcode sqlite


    【解决方案1】:

    这里发生了一些事情......

    1- 您没有从 sql 查询中选择主键。您需要将其更改为“SELECT pk, ti​​tle....”之类的内容才能检索到它。

    2- 您只是将标题存储在您的品牌中。理想情况下,您希望创建一些存储在数组中的数据结构(可能是一个类),以便稍后访问 PK。

    3- 您需要在 UITableView 上实现 didSelectRowAtIndexPath 方法以确定在 tableview 中触摸了哪一行。

    一旦你解决了这些问题......你想要做的就是将一个配置好的(即:你已经告诉它是哪一个)ModelTableViewController 推送到你的导航控制器上。

    您应该看看开发者网站上的 Elements 示例。这是使用 SQLite 填充 UITableView 的一个非常好的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-16
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      相关资源
      最近更新 更多