How can I set text for cell in UITableview from NSDictionary
I have an NSDictionary with 5 keys and corresponding values. How 开发者_如何学运维can I set the value in the cell of UITableView when I don't know the key?
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;
}
精彩评论