将plist加载到iOS TableView中 [英] Loading plist into iOS TableView
问题描述
我有一个包含以下内容的plist( images.plist )
I have a plist (images.plist) with the following contents
如您所见,每个项目都有一个数字键,范围是0到19.每个项目还具有两个字符串(fileName和fileInfo).
As you can see, each item has a numerical key, from 0-19. Each item also has two strings (fileName and fileInfo).
我正在尝试将所有文件名加载到TableView中.这是我的尝试:
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];
这是我尝试将fileName加载到原型单元中的方式.
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;
}
注意
记录,我的CellIdentifier是正确的,如果将cell.textLabel.text设置为@"HELLO!"
,那么我将看到"HELLO!".对于NSDictionary中的每个项目.我在//Configure the 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.
更新
尝试使用我从下面的答案中学到的知识,
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;
但这给了我类似的结果
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)
如果我更改此行:
NSDictionary *dict = [self.roster objectForKey:[NSNumber numberWithInt:indexPath.row]];
收件人:
NSDictionary *dict = [self.roster objectForKey:@"5"];
然后,所有单元格的第6个元素都将具有正确的fileName.知道为什么[NSNumber numberWithInt:indexPath.row
无法正常工作吗?
Then all of the cells will have the correct fileName for the 6th element. Any idea why [NSNumber numberWithInt:indexPath.row
isn't working?
推荐答案
您可以执行以下操作:
NSDictionary *dict = [self.roster objectForKey:indexPath.row];
NSString *fileName = [dict objectForKey:@"fileName"];
这篇关于将plist加载到iOS TableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!