2012-01-05 63 views
0

我有一個NSArrayNSDictionaries其中每個字典都有相同的密鑰。我想爲數組中的每個字典設置一個部分。使用NSArray加載UITableView

例如,如果數組有4個字典,則該表將有4個部分。然後每個部分中的每個單元格將與字典中的該值相對應。

是否有一個簡單的方法來做到這一點?

例如,我有一個帶有3個詞典的NSArray,其中每個詞典都有3個鍵,名稱,性別和日期。

這意味着該表應該有3個部分,其中每個部分的標題是其字典的日期鍵。然後,該部分有2個單元對應於名稱和性別值。

+0

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html請參閱:numberOfSections,numberOfRowsInSection – 2012-01-06 00:28:27

+0

雅我很熟悉這些方法,但我'不知道如何從'titleForHeaderInSection'方法正確設置字典中的日期鍵。 – Jon 2012-01-06 00:30:31

回答

1

雅我familar與方法,但我不知道怎麼去從字典正確地安裝在 titleForHeaderInSection方法 日期鍵。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    return (NSString *)[myArray objectAtIndex:section] objectForKey:@"date"]; 
} 

這完美地工作。但我怎樣才能以類似的方式編輯cellForRowAtIndexPath中的代碼?

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

    NSString *value = @"Unknown"; 
    if (indexPath.row == 0){ 
     value = (NSString *)[[myArray objectAtIndex:indexPath.section] objectForKey:"name"]; 
    }else if (indexPath.row == 1){ 
     value = (NSString *)[[myArray objectAtIndex:indexPath.section] objectForKey:@"gender"]; 
    } 

    myCell.textPropertyOrWhatever = value; 
    return myCell; 
} 

你甚至想這個呢? :P

+0

這工作完美無瑕。但是,我怎樣才能以類似的方式編輯'cellForRowAtIndexPath'中的代碼? – Jon 2012-01-06 00:43:25