2013-10-04 50 views
0

我有一个plist,它是一个有字典的数组。我试图在表格视图中输出名称,但名称不显示。我至今是下面的代码plist value not displayed in tableview

self = [super init]; 
if (self) { 
    // Fetch from the app bundle... load the NSDictionary 
    _qList = [NSArray arrayWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"plist"]]; 

    _qNames=[_qList valueForKey:@"Name"]; 


} 
return self; 

而对于我的表视图,我有

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 


    int index = [indexPath indexAtPosition:1]; 
NSString *key = [[self.test.qNames valueForKey:@"Name"] objectAtIndex:index]; 
NSString *value = [self.test.qNames objectForKey:key]; 
[[cell textLabel] setText:value]; 

    return cell; 
} 

我的plist结构如下:

根(阵列) -Item 0(字典) -Name -Class -Age -Item 1(Dictionary) -Name - Class - 年龄

等..

我只需要弄清楚如何把

NSDictionary *name=[self.model.qList valueForKey:@"Name"]; 

我返回到表视图的名字吗?

+0

_qNames = [_的QList valueForKey:@ “名称”];正在返回名称 – user2456195

+0

如果qList是一个数组,为什么使用valueForKey?你不想使用objectAtIndex:得到一个字典,然后开始挑选单个值? –

+0

_qNames = [_ qList valueForKey:@“Name”];正在返回我在我的plist中的20个名字。 qlist是一个数组,但在该数组中,我有一个字典 – user2456195

回答

1

我想你正在尝试使用键值编码(KVC)从字典中获取名称(字符串)的数组。

对于您应该使用valueForKeyPath,不valueForKey

在初始化,

//Get the array of names 
_qNames=[_qList valueForKeyPath:@"Name"]; 

在的cellForRowAtIndexPath,

NSString *name = [self.test.qNames objectAtIndex:indexPath.row]; 
[[cell textLabel] setText:name];