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

Iphone 如何从NSDictionary为UITableview中的单元格设置文本,iphone,Iphone,我有一个NSDictionary,有5个键和相应的值。当我不知道键时,如何在UITableView的单元格中设置值 d=[[NSDictionary alloc]initWithObjectsAndKeys:@“Air Bus”,“ab”, @“波音787”,“787”,“一些飞机”, @“sp”,无]您可以在字典中显示键列表,如下所示: for (id key in [myDictionary allKeys]) NSLog(@"Key : %@ => value : %@", ke

我有一个NSDictionary,有5个键和相应的值。当我不知道键时,如何在UITableView的单元格中设置值

d=[[NSDictionary alloc]initWithObjectsAndKeys:@“Air Bus”,“ab”,
@“波音787”,“787”,“一些飞机”,

@“sp”,无]

您可以在字典中显示键列表,如下所示:

for (id key in [myDictionary allKeys])
  NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]);
- (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;
}
所以,对于你的字典,这将显示

ab => Air Bus
787 => Boeng 787
sp => Some Plane
我假设您希望在一个表中显示所有值,因此您将需要如下内容:

for (id key in [myDictionary allKeys])
  NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]);
- (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;
}

您可以在字典中显示键列表,如下所示:

for (id key in [myDictionary allKeys])
  NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]);
- (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;
}
所以,对于你的字典,这将显示

ab => Air Bus
787 => Boeng 787
sp => Some Plane
我假设您希望在一个表中显示所有值,因此您将需要如下内容:

for (id key in [myDictionary allKeys])
  NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]);
- (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;
}

你需要发布一些代码来帮助我们!NSDictionary是从哪里来的?你需要发布一些代码来帮助我们!NSDictionary从何而来?谢谢,但需要在表格cellForRowAtIndexPath中的每一行显示它抱歉,我在您编辑问题时回答了-请参阅我的新编辑!谢谢,但需要在表格cellForRowAtIndexPath中的每一行显示它对不起,我在您编辑问题时回答了-请参阅我的新编辑!