首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >plist值未显示在表视图中

plist值未显示在表视图中
EN

Stack Overflow用户
提问于 2013-10-04 08:22:26
回答 1查看 76关注 0票数 0

我有一个plist,它是一个数组,里面有字典。我试图打印出表视图中的名称,但是名称没有显示出来。到目前为止,我得到的是以下代码

代码语言:javascript
复制
    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;

对于我的表视图,我有

代码语言:javascript
复制
    - (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(字典) -Name -Class -Age

以此类推..

我只需要弄清楚怎么把

代码语言:javascript
复制
 NSDictionary *name=[self.model.qList valueForKey:@"Name"];

我返回到表视图中的名称?

EN

回答 1

Stack Overflow用户

发布于 2013-10-04 13:22:38

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

为此,您应该使用valueForKeyPath,而不是valueForKey

在初始化器中,

代码语言:javascript
复制
//Get the array of names
 _qNames=[_qList valueForKeyPath:@"Name"];

在cellForRowAtIndexPath中,

代码语言:javascript
复制
NSString *name = [self.test.qNames objectAtIndex:indexPath.row];
[[cell textLabel] setText:name];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19171228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档