简体   繁体   English

如何从NSDictionary为UITableview中的单元格设置文本

[英]How can I set text for cell in UITableview from NSDictionary

I have an NSDictionary with 5 keys and corresponding values. 我有一个带有5个键和相应值的NSDictionary。 How can I set the value in the cell of UITableView when I don't know the key? 当我不知道密钥时,如何在UITableView的单元格中设置值?

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

You can show a list of the keys in the dictionary like this : 您可以在字典中显示键的列表,如下所示:

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

so, for your dictionary this would show 所以,对于你的字典,这将显示

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

I'm assuming that you want to show all the values in a table so you will want something like this : 我假设你想要在表中显示所有值,所以你会想要这样的东西:

- (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;
}

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

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