0

我在循环中有这样的数据结构,生成的名为 self.practice 的数组的内容应该在 tableviewController 中填充动态表视图

    // 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"];
4

1 回答 1

0

您的self.practicecontains 数组,即practiceOverviewData您的practiceOverviewDatacontains 字典:

[self.practice objectAtIndex: n] = practiceOverviewData

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

像这样访问数据:

[[[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"];
    }
}

在 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可能包含不同类型的数据。

于 2012-09-18T05:42:54.410 回答