2010-08-02 40 views
1

我有一個包含5個鍵和相應值的NSDictionary。當我不知道密鑰時,如何設置UITableView單元格中的值?如何從NSDictionary設置UITableview中的單元格的文本

d=[[NSDictionary alloc] initWithObjectsAndKeys:@"Air Bus",@"ab", @"Boeing 787 ",@"787",@"Some Plane", @"sp",nil];

+0

你需要發佈一些代碼給我們來幫助! NSDictionary來自哪裏? – deanWombourne 2010-08-02 10:37:36

+0

請檢查驗證碼。 – iPhoneDev 2010-08-02 10:40:25

回答

4

您可以顯示在字典這樣的鍵列表:

for (id key in [myDictionary allKeys]) 
    NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]); 

如此,對於你的字典,這將表明

ab => Air Bus 
787 => Boeng 787 
sp => Some Plane 

我假設你想顯示錶中的所有值,所以你會想要這樣的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Get a cell 
    ... 

    // Get the data to display for this cell 
    int index = [indexPath indexAtPosition:1]; 
    NSString *key = [[d allKeys] objectAtIndex:index]; 
    NSString *value = [d objectForKey:key]; 
    [[cell textLabel] setText:value]; 

    return cell; 
} 
+0

謝謝,但需要在表格cellForRowAtIndexPath中顯示每行 – iPhoneDev 2010-08-02 10:43:08

+0

抱歉,我在編輯問題時回答 - 請參閱我的新編輯! – deanWombourne 2010-08-02 10:46:29

相關問題