views:

84

answers:

1

If I was adding data from an array to the UITableView datasource array I'd use this, in viewDidLoad.

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

And this in cellForRowAtIndexPath

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

But I want to link up the results from a select query, I'm using fmdb to access my database. Heres how I output data to the console with fmdb at the moment.

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];

How do i do this ?

+1  A: 

Build a transaction array in the while loop like this:

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

EDIT: Yup thats right, to accesses it just use:

descLabelInTableView = [[arrayTmp objectAtIndex:indexPath.row] objectForKey:@"desc"];
Rudiger
dictionary label? can you complete your example with my fields ?Can you also show me what would go in cellForRowAtIndexPath ?
Jules
Is this correct ? [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:[rs stringForColumn:@"desc"],@"desc", [rs stringForColumn:@"due"],@"due", [rs stringForColumn:@"price"],@"price", [rs stringForColumn:@"accumulated_price"],@"accum",nil]];If so how do I access each item from cellForRowAtIndexPath ?
Jules
See edit in answer
Rudiger