objective-c访问数组中的键值以进行动态tableview

时间:2012-09-18 05:32:08

标签: ios

我在循环中得到了这样的数据结构,生成的数组的内容名为self.practice,应填充tableviewController中的动态tableview

    // new NSDictionary
    NSDictionary *highway = [[NSDictionary alloc] initWithObjectsAndKeys:@"Highway", @"description",
                              @"H", @"namekey",
                              [NSString stringWithFormat:@"%i", aHours], @"hours",
                              aHoursDefault.stringValue, @"hoursdefault",
                              [self.classlist objectAtIndex:i], @"driverclass",
                              nil];
    NSDictionary *freeway = [[NSDictionary alloc] initWithObjectsAndKeys:@"Freeway", @"description",
                              @"F", @"namekey",
                              [NSString stringWithFormat:@"%i", fHours], @"hours",
                              fHoursDefault.stringValue, @"hoursdefault",
                              [self.classlist objectAtIndex:i], @"driverclass",
                              nil];

    NSDictionary *stateHigway = [[NSDictionary alloc] initWithObjectsAndKeys:@"stateHighway", @"description",
                              @"S", @"namekey",
                              [NSString stringWithFormat:@"%i", sHours], @"hours",
                              sHoursDefault.stringValue, @"hoursdefault",
                              [self.classlist objectAtIndex:i], @"driverclass",
                              nil];



    // Values of Dictionary assign to array

    [self.practiceOverviewData addObject:highway];
    [self.practiceOverviewData addObject:freeway];
    [self.practiceOverviewData addObject:stateHighway];



    // put those arrays in the "Big Array" for my tableview
    [self.practice addObject:self.practiceOverviewData];

现在在我的cellForRowAtIndexPath中:我无法通过内部数组访问键值

    cell.textLabel.text = [[self.practice objectAtIndex:indexPath.row] objectForKey:@"description"];

1 个答案:

答案 0 :(得分:0)

您的self.practice包含数组,practiceOverviewDatapracticeOverviewData包含字典:

  

[self.practice objectAtIndex:n] = practiceOverviewData

     

[[self.practice objectAtIndex:n] objectAtIndex:n] =   公路/高速公路/ statehiway

访问这样的数据:

[[[self.practice objectAtIndex: n] objectAtIndex:indexPath.row] objectForKey:@"description"];

编辑:

[self.practiceOverviewData addObject:highway];
[self.practiceOverviewData addObject:freeway];
[self.practiceOverviewData addObject:stateHighway];

NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectAndKey:
                               self.practiceOverviewData,@"roads_data",nil];

[self.practice addObject:newDictionary];

NSArray *data = [NSArray array];
for (int i = 0; i < [practice count]; i++) {
    if ([[self.practice objectAtIndex:i] isKindOfClass:[NSDictionary class]]) {
        data = [(NSDictionary*)[self.practice objectAtIndex:i]objectForKey:@"road_data"];
    }
}

in cellForRowAtIndexPath:..

 //of course you want to declare data in your header file to access it in your cellForRow..
cell.textLabel.text = [(NSDictionary*)[data objectAtIndex:indexPath.row] objectForKey:@"description"];

编辑:此代码假定您的self.practice可能包含不同类型的数据。