2012-10-26 50 views
0

我已經創建了一個將列出名稱的應用程序。無論何時點擊該名稱,我都希望返回特定名稱的年齡。我正在動態添加名稱和年齡。如果我使用Key-Value對,那麼檢索我猜測的值會更容易。任何人都可以告訴我如何在這裏使用KeyValue(NSDictionary)對?使用鍵值對UITableView

回答

1
NSDictionary *nameAgePair=[[NSDictionary alloc]init]; 
[nameAgePair setValue:@"30" forKey:@"Name1"]; 
[nameAgePair setValue:@"25" forKey:@"Name2"]; 

NSArray *arr=[nameAgePair allKeys]; 


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

    UITableViewCell *cell = [[tableView dequeueReusableCellWithIdentifier:@"cell"] autorelease]; 

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
    [lbl setText:[arr objectAtIndex:indexPath.row]]; 
    [cell.contentView addSubview:lbl]; 

} 

您可以像這樣訪問選定的值。

[nameAgePair objectForKey:@"selected row"]; 

注意:如果使用鍵值編碼,鍵(名稱)應該是唯一的。