开发者

Problems displaying array from plist in tableview cell

I want to read and display my arrays in my Stages dict into the tableview. However, my app crashes and give the error message: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4b43ee0'

I am able to display the number of rows for the arrays in the Stages dict but not able to display the name inside the table cell.

Below is my plist a开发者_如何学Cnd codes.

Root (Array)

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

In my viewDiDLoad method, i called

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

tableview methods

- (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;
    }

Thanks in advance(:


the stages element is a dictionary, not an array. I'm guessing that each element in the dictionary are array objects, and you want to display the keys in the table view.

Also, the plist given doesn't seem to match the example. You specify Stage in the dictionary labelled 'course', but in the plist, it shows Stages

Perhaps you want to try:

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


In your plist file the project name variable appears like this: Project Name and in this line: cell.detailTextLabel.text = [course objectForKey:@"ProjectName"];; it appears like this: ProjectName. I think both have to be the same. Maybe this appears only here.

Your error appear because the stages variable is a dictionary as you can see in your PList file, but you try to use it as an array. The stages dictionary contains 4 keys as I see: Analysis, Design, Develop, Design (strange, same keys in Dictionary???). So you have to use the - (id)objectForKey:(id)aKey; method first, with one of the keys in the Stages dictionary and then after this, you can use the objectAtIndex: method.


NSString *cellvalue = [stages.allValues objectAtIndex:indexPath.row];

note the "allValues". allValues returns an array of all the values in the dictionary, allKeys returns an array of all the keys in the dictionary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜