【问题标题】:iPhone + sqlite3 + fmdb, What code do i need to put data into a UiTableView datasource?iPhone + sqlite3 + fmdb,我需要什么代码将数据放入 UiTableView 数据源?
【发布时间】:2011-04-16 21:12:13
【问题描述】:

如果我将数组中的数据添加到 UITableView 数据源数组中,我会在 viewDidLoad 中使用它。

NSMutableArray *array = [[NSArray alloc] initWithObjects:@"Head First Design Patterns", @"Head First HTML & CSS", @"Head First iPhone", nil];
self.transactionsArray = array;
[array release];

这个在 cellForRowAtIndexPath 中

NSInteger row = [indexPath row];
cell.textLabel.text = [transactionsArray objectAtIndex:row];

但我想链接来自选择查询的结果,我正在使用 fmdb 来访问我的数据库。以下是我目前使用 fmdb 将数据输出到控制台的方式。

FMDatabase* db = [FMDatabase databaseWithPath:@"/tmp/mydb.db"];
if (![db open]) {
    NSLog(@"Could not open db.");
}

FMResultSet *rs = [db executeQuery:@"select * from myTable",  nil];
while ([rs next]) {
    NSLog(@"%@, %@, %@, %@, %@", [rs stringForColumn:@"pid"],
            [rs stringForColumn:@"desc"], 
            [rs stringForColumn:@"due"],
            [rs stringForColumn:@"price"],
            [rs stringForColumn:@"accumulated_price"]);
}
[rs close]; 
[db close];

我该怎么做?

【问题讨论】:

    标签: iphone sqlite uitableview iphone-sdk-3.0 fmdb


    【解决方案1】:

    在 while 循环中构建一个事务数组,如下所示:

    [transactionArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[rs stringForColumn@"pid", @"dictionaryLabel",........, nil];
    

    编辑:没错,访问它只需使用:

    descLabelInTableView = [[arrayTmp objectAtIndex:indexPath.row] objectForKey:@"desc"];
    

    【讨论】:

    • 字典标签?你能用我的领域完成你的例子吗?你能告诉我 cellForRowAtIndexPath 会发生什么吗?
    • 这是正确的吗? [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:[rs stringForColumn:@"desc"],@"desc", [rs stringForColumn:@"due"],@"due", [rs stringForColumn:@"price"],@" price", [rs stringForColumn:@"accumulated_price"],@"accum",nil]];如果是这样,我如何从 cellForRowAtIndexPath 访问每个项目?
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2012-04-24
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多