【问题标题】:plist value not showing in tableviewplist 值未显示在 tableview 中
【发布时间】:2013-10-04 00:22:25
【问题描述】:

我有一个 plist,它是一个包含字典的数组。我正在尝试在表格视图中打印出名称,但是名称没有显示出来。到目前为止我所拥有的是以下代码

    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结构如下:

根(数组) -第 0 项(字典) -姓名 -班级 -年龄 -第 1 项(字典) -姓名 -班级 -年龄

等等..

我只需要弄清楚如何放置

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

我返回表格视图的名称?

【问题讨论】:

  • _qNames=[_qList valueForKey:@"Name"];正在返回名称
  • 如果 qList 是一个数组,为什么要使用 valueForKey?您不想使用 objectAtIndex: 来获取字典之一,然后开始挑选单个值吗?
  • _qNames=[_qList valueForKey:@"Name"];正在返回我在 plist 中的 20 个名称。 qlist 是一个数组,但在那个数组里面我有一个字典

标签: ios plist tableview


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多