开发者

Objective-C NSDictionary - Ordering keys and objects

开发者 https://www.devze.com 2023-03-20 16:15 出处:网络
const char *sql = [[[NSString alloc] initWithFormat:@\"Select * from table1 where id = \'%@\' limit 1\",str] UTF8String];
const char *sql = [[[NSString alloc] initWithFormat:@"Select * from table1 where id = '%@' limit 1",str] UTF8String];

if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) != SQLITE_OK)
    NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));

NSMutableArray *keys = [NSMutableArray array];
NSMutableArray *objects = [NSMutableArray array];
NSString *keyText = [NSString alloc];

while(sqlite3_step(detailStmt) == SQLITE_ROW) {

    for (int x=1; x<=74;x++)
    {

        switch (x) {
            case 1:
                keyText = @"Title";
                break;
            case 2:
                keyText = @"Year";
                break;
            case 3:
                keyText = @"Reference";
                break;
            case 4:
                keyText = @"Category";
                break;
            default:
                break;
        }


        if([[NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, x)] isEqualToString:@""]){
            //Don't add empty fields to the array
        } else { 

        [objects addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, x)]];  

        [keys addO开发者_StackOverflow社区bject:keyText];


        }
    }
 }

appDelegate.itemList = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

sqlite3_reset(detailStmt);

The above code gets some information out of a SQLite database, there are 74 fields of information, I am then adding the keys (field name) to a 'keys' array, and the content of the field to 'objects' array. I am then adding both keys and objects to an NSDictionary, I can then view this in a table view like so:

NSArray* allKeys = [appDelegate.itemList allKeys];

cell.titleLbl.text = [allKeys objectAtIndex:indexPath.row];
cell.descLbl.text = [appDelegate.itemList objectForKey:[allKeys objectAtIndex:indexPath.row]];

What I need to do is order the keys with corresponding objects in a particular order for display in my table view, the order that the fields are in in the database is not necessarily the way I'd like them to be displayed. For instance I want to show Category, Reference, Title, then Year in my table view - so I should be adding them to my NSDictionary in that order I presume. How is the best way to go about this?


That's not how NSDictionary works. The order of keys (and values) in the dictionary is arbitrary; and not at all related to the order in which keys are added.

The correct way to do this is to attach relevant information for looking up the value to the NSTableColumns, and using that info to look it up when the tableview loads its data.

Doing this correctly automagically causes columns being reordered in the interface to work, etc..

0

精彩评论

暂无评论...
验证码 换一张
取 消