2012-09-18 49 views
0

我有像這樣的數據結構在一個循環中,稱爲self.practice所產生的陣列的含量應在tableviewController目標c訪問關鍵值陣列,用於動態的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]; 
填充一個動態的tableview

現在,在我的cellForRowAtIndexPath:我無法通過

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

回答

0

訪問從內陣列中的鍵值你self.practice包含數組,這是practiceOverviewData和你practiceOverviewData包含詞典:

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

中的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可能包含不同種類的數據。

+0

感謝,迄今爲止工作,但我如何正確設置我的cellForRowAtIndexPath self.practice正確的索引:? indexPath.row不工作。 –

+0

我編輯了答案。你可以試試那個。 – janusbalatbat

+0

不工作 - ... self.practice objectForKey:沒有可見的接口 - 我應該再次發佈完整的代碼? –