简体   繁体   English

对UITableView使用键值对

[英]Use Key Value pair for the UITableView

I have created a application which will list the names . 我创建了一个将列出名称的应用程序。 Whenever the name is tapped , I want to return the age of the particular name. 每当点击名称时,我都想返回特定名称的年龄。 I am adding the names and age dynamically. 我正在动态添加姓名和年龄。 If I use Key-Value pair, it will be easier to retrieve the value I guess. 如果我使用键/值对,则检索我猜想的值会更容易。 Can anyone tell me how to use the KeyValue(NSDictionary) pair here ? 有人可以在这里告诉我如何使用KeyValue(NSDictionary)对吗?

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

}

You can access the selected values like this. 您可以像这样访问选定的值。

[nameAgePair objectForKey:@"selected row"];

Note: If you use key-value coding your keys(names) should be unique. 注意:如果您使用键值编码,则您的键(名称)应该是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM