【问题标题】:Problems displaying array from plist in tableview cell在 tableview 单元格中显示来自 plist 的数组的问题
【发布时间】:2011-08-23 16:10:52
【问题描述】:

我想在我的 Stages dict 中读取我的数组并将其显示到 tableview 中。但是,我的应用程序崩溃并给出错误消息:* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4b43ee0'

我能够在 Stages 字典中显示数组的行数,但无法在表格单元格中显示名称。

下面是我的 plist 和代码。

根(数组)

 Item 0 (Dict)
        Project Name (String)
        Stage (Dict)
                Analysis (Array)
                Design (Array)
                Develop (Array)
                Implement (Array)

在我的 viewDiDLoad 方法中,我调用了

    //course is a nsdictionary which is pass down from the previous view
    stages = [course objectForKey:@"Stage"];

表格视图方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = 0;

if (section == 0){
    rows = 1;
}

if (section == 1) {
    return [stages count];
}
return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"StagesCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {      
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
}   
// Set up the cell

if (indexPath.section == 0) {
    if (indexPath.row == 0) {
        cell.textLabel.text = @"Project Name";
        cell.detailTextLabel.text = [course objectForKey:@"ProjectName"];;
    } 
}

if (indexPath.section == 1) {
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   

    NSString *cellvalue = [stages objectAtIndex:indexPath.row];     
    cell.textLabel.text = cellvalue;
}   
return cell;
    }

提前致谢(:

【问题讨论】:

    标签: iphone objective-c ios uitableview plist


    【解决方案1】:

    stages 元素是字典,而不是数组。我猜测字典中的每个元素都是数组对象,而您想在表格视图中显示键。

    此外,给出的 plist 似乎与示例不匹配。您在标有“课程”的字典中指定 Stage,但在 plist 中,它显示 Stages

    也许你想试试:

    stages = [[course objectAtIndex:@"Stages"] allKeys];
    

    【讨论】:

      【解决方案2】:

      在您的 plist 文件中,项目名称变量如下所示:Project Name,在这一行中:cell.detailTextLabel.text = [course objectForKey:@"ProjectName"];; 如下所示:ProjectName。我认为两者必须相同。也许这只出现在这里。

      您的错误出现是因为 stage 变量是一个字典,正如您在 PList 文件中看到的那样,但您尝试将它用作数组。正如我所见,stages 字典包含 4 个键:Analysis、Design、Develop、Design(奇怪,Dictionary 中的键相同???)。所以你必须先使用- (id)objectForKey:(id)aKey;方法,使用Stages字典中的一个键,然后再使用objectAtIndex:方法。

      【讨论】:

      • 感谢您的回复。我犯了一些拼写错误并编辑了它们(:嗯,但我已经在我的 viewdidload 中使用了 objectforkey 方法,还是你的意思是我必须在 cellforrowatindex 中再次使用该方法?谢谢
      • 是的,你必须这样做,因为你的 course 变量是一个字典,所以 objectforkey 很好,但 stage 变量也是一个字典,所以你也必须在那里使用 objectforkey。
      【解决方案3】:
      NSString *cellvalue = [stages.allValues objectAtIndex:indexPath.row];
      

      注意“所有值”。 allValues 返回字典中所有值的数组, allKeys 返回字典中所有键的数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多