将plist加载到iOS TableView中
我有一个包含以下内容的plist(images.plist) 如您所见,每个项目都有一个数字键,从0到19。每个项目还有两个字符串(fileName和fileInfo) 我正在尝试将所有文件名加载到TableView中。以下是我的尝试: RosterMasterViewController.h将plist加载到iOS TableView中,ios,uitableview,plist,nsdictionary,Ios,Uitableview,Plist,Nsdictionary,我有一个包含以下内容的plist(images.plist) 如您所见,每个项目都有一个数字键,从0到19。每个项目还有两个字符串(fileName和fileInfo) 我正在尝试将所有文件名加载到TableView中。以下是我的尝试: RosterMasterViewController.h @interface RosterMasterViewController : UITableViewController @property (nonatomic, strong) NSDictio
@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];
- (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;
}
下面是我如何将文件名加载到原型单元格中的
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];
- (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!”
,那么我将在NSDictionary中看到每个项目的“HELLO!”。我在//配置单元格
部分遇到困难
不幸的是,这并不像我预期的那样有效。我觉得我的钥匙都是数字的,所以我有点困难
更新
尝试使用我从以下答案中学到的知识,我有以下几点:
// 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]];
致:
然后,所有单元格都将具有第6个元素的正确文件名。知道为什么[NSNumber numberwhint:indexPath.row
不起作用吗?您可以这样做:
NSDictionary *dict = [self.roster objectForKey:indexPath.row];
NSString *fileName = [dict objectForKey:@"fileName"];
正如Oscar所指出的,self.Floster是一本NSDictionary,里面有每个数字键的字典 您必须首先检索数字键的NSDictionary:
NSDictionary*fileDictionary=[self.lotster objectForKey:indexPath.row];
之后,必须从最后一个字典中提取文件名,因此必须请求@“filename”键的字符串
不确定你是否已经解决了这个问题,但下面是我如何解决这个问题的
NSDictionary *dict =
[self.allItem objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]];
我认为原因是[NSNumber numberwhithint:indexPath.row]
正在返回number/int值。
但是objectForKey:
希望收到一个字符串值
希望有帮助。我希望
self.lotster=[NSDictionary dictionary WithContentsOfFile:file];
返回带有整数键的字典,但我仍然不知道如何从每个字典访问文件名。谢谢,这似乎可以,但第一行:NSDictionary*dict=[self.lotus objectForKey:indexPath.row];
给了我一个错误:隐式转换“NSInteger”(也称为int)ARC不允许使用“id”。我正在使用ARC,你能帮我弄清楚为什么我不能这样做吗?@ardavis ARC不允许你这样做,因为它需要知道它是否将提前处理对象或原语。你可以使用以下命令:NSDictionary*dict=[self.lotster objectForKey:[NSNumber numberwhithint:indexPath.row]];很抱歉一直打扰您。NSDictionary*dict=[self.花名册objectForKey:@“0”];
工作,返回正确的字典。但是NSDictionary*dict=[self.花名册objectForKey:[NSNumber numberwhithint:indexath.row];
返回空值。我是这方面的初学者!用:NSDictionary*dict=[self.lotster objectForKey:[NSString stringWithFormat:@“%i”,indexPath.row]];
我会将您的标记为正确,因为它引导我找到了正确的答案:)