问题
I have a plist (images.plist) with the following contents

As you can see, each item has a numerical key, from 0-19. Each item also has two strings (fileName and fileInfo).
I'm trying to load all of the fileName's into a TableView. Here's my attempt:
RosterMasterViewController.h
@interface RosterMasterViewController : UITableViewController
@property (nonatomic, strong) NSDictionary *roster;
@end
RosterMasterViewController.m
@implementation RosterMasterViewController
@synthesize roster = _roster;
...
// This is in my 'viewDidLoad'
NSString *file = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"];
self.roster = [NSDictionary dictionaryWithContentsOfFile:file];
And here's how I'm trying to load the fileName into the Prototype Cells.
RosterMasterViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"imageNameCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.textLabel.text = [[[self.roster allKeys] objectAtIndex:indexPath.row] objectForKey:@"fileName"];
return cell;
}
NOTE
For the record, my CellIdentifier is correct, if I set the cell.textLabel.text to be @"HELLO!"
, then I will see "HELLO!" for each item in the NSDictionary. I'm having difficulty with the //Configure the cell
section
Unfortunately this isn't working as I expected. I'm having difficulty since my keys are all numerical I think.
UPDATE
Trying to use what I've learned from the answers below, I have this:
// Configure the cell
NSLog(@"Key: %@", [NSNumber numberWithInt:indexPath.row]);
NSDictionary *dict = [self.roster objectForKey:[NSNumber numberWithInt:indexPath.row]];
NSLog(@"Dictionary: %@", dict);
NSString *fileName = [dict objectForKey:@"fileName"];
NSLog(@"FileName: %@", fileName);
cell.textLabel.text = fileName;
return cell;
But that's giving me results like:
2012-02-03 11:24:24.295 Roster[31754:f803] Key: 7
2012-02-03 11:24:24.295 Roster[31754:f803] Dictionary: (null)
2012-02-03 11:24:24.296 Roster[31754:f803] FileName: (null)
If I change this line:
NSDictionary *dict = [self.roster objectForKey:[NSNumber numberWithInt:indexPath.row]];
to:
NSDictionary *dict = [self.roster objectForKey:@"5"];
Then all of the cells will have the correct fileName for the 6th element. Any idea why [NSNumber numberWithInt:indexPath.row
isn't working?
回答1:
You can do this:
NSDictionary *dict = [self.roster objectForKey:indexPath.row];
NSString *fileName = [dict objectForKey:@"fileName"];
回答2:
As pointed out by Oscar, self.roster it's a NSDictionary wich have inside of him dictionaries for each numeric key.
You must first retrieve the NSDictionary for the numeric key: NSDictionary *fileDictionary = [self.roster objectForKey:indexPath.row];
After that you must extract your filename from this last dictionary so you must request the string for the @"fileName" key.
NSString *fileName = [fileDictionary objectForKey:@"fileName"];
cell.textLabel.text = fileName;
return cell;
回答3:
No sure if you solved the problem already, but below is how I solved this issue.
NSDictionary *dict =
[self.allItem objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]];
I think the reason was [NSNumber numberWithInt:indexPath.row]
is returning number/int value.
But objectForKey:
is expecting to receive a string value.
Hope this help.
来源:https://stackoverflow.com/questions/9130259/loading-plist-into-ios-tableview