我有一个带有5个键和相应值的NSDictionary。当我不知道密钥时,如何在UITableView的单元格中设置值?
d=[[NSDictionary alloc] initWithObjectsAndKeys:@"Air Bus",@"ab",
@"Boeing 787 ",@"787",@"Some Plane",
@"sp",nil];
答案 0 :(得分:4)
您可以在字典中显示键的列表,如下所示:
for (id key in [myDictionary allKeys])
NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]);
所以,对于你的字典,这将显示
ab => Air Bus
787 => Boeng 787
sp => Some Plane
我假设你想要在表格中显示所有值,所以你会想要这样的东西:
- (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;
}