2017-06-29 25 views
-1

我要聲明一個二維陣列,其中我知道我需要的列數,但要在程序運行時將行添加到每一列的二維陣列。我想讓數組保存NSMutableDictionary對象。我將如何做到這一點?的iOS - 聲明與固定柱和未知的行(目標C)

UPDATE: 由二維數組但調用對象時崩潰:

for (int i = 0; i < [resultSet count]-1; ++i){ 
        NSMutableDictionary *temp = (NSMutableDictionary *) [resultSet objectAtIndex:i]; 
        if (temp == nil) break; // Break if nil 
        NSMutableDictionary * currentEntry = [[NSMutableDictionary alloc] init]; 
        //store the firstname, lastname, userid, starting and ending dates 

        [currentEntry setObject:[temp objectForKey:@"ticket_id"] forKey:@"ticket_id"]; 
        [currentEntry setObject:[temp objectForKey:@"title"] forKey:@"title"]; 
        [currentEntry setObject:[temp objectForKey:@"user_id"] forKey:@"user_id"]; 
        [currentEntry setObject:[temp objectForKey:@"date_submitted"] forKey:@"date_submitted"]; 
        [currentEntry setObject:[temp objectForKey:@"date_resolved"] forKey:@"date_resolved"]; 
        [currentEntry setObject:[temp objectForKey:@"handled_by"] forKey:@"handled_by"]; 
        [currentEntry setObject:[temp objectForKey:@"status"] forKey:@"status"]; 
        [currentEntry setObject:[temp objectForKey:@"category"] forKey:@"category"]; 
        [currentEntry setObject:[temp objectForKey:@"priority"] forKey:@"priority"]; 
        [currentEntry setObject:[temp objectForKey:@"description"] forKey:@"description"]; 
        [currentEntry setObject:[temp objectForKey:@"resolution"] forKey:@"resolution"]; 
        //stored in db in enum as "school" this translates it to "campus" 
        //maintains backwards compatability with db and previous app versions 
        [currentEntry setObject:[temp objectForKey:@"location"] forKey:@"location"]; 
        [currentEntry setObject:[temp objectForKey:@"date_modified"] forKey:@"date_modified"]; 
        [currentEntry setObject:[temp objectForKey:@"room_no"] forKey:@"room_no"]; 

        [currentEntry setObject:[temp objectForKey:@"submitter_name"] forKey:@"submitter_name"]; 
        [currentEntry setObject:[temp objectForKey:@"submitter_surname"] forKey:@"submitter_surname"]; 

        [currentEntry setObject:[temp objectForKey:@"item_type"] forKey:@"item_type"]; 
        NSLog(@"ITEMS: %@", [currentEntry objectForKey:@"item_type"]); 
        int k = 0; 
        for (int i = 0; i < 28; i++) { 
         if ([[currentEntry objectForKey:@"item_type"] isEqualToString:items[i]]) { 
          myArray[i] += 1; 
          k = i; 
         } 
        } 

2D陣列的聲明:

    multiArray = [NSArray arrayWithObjects: 
            [NSMutableArray array], 
            [NSMutableArray array], 
            [NSMutableArray array], 
            [NSMutableArray array], 
            [NSMutableArray array], 
            [NSMutableArray array], 
            [NSMutableArray array], 
            [NSMutableArray array],nil]; 

更多的代碼:

    if ([[currentEntry objectForKey:@"handled_by"] isEqualToString:@"unassigned"]) { 
         [currentEntry setObject:@"unassigned" forKey:@"handler_name"]; 
         [currentEntry setObject:@"" forKey:@"handler_surname"]; 
        } 
        else { 
         [currentEntry setObject:[temp objectForKey:@"handler_name"] forKey:@"handler_name"]; 
         [currentEntry setObject:[temp objectForKey:@"handler_surname"] forKey:@"handler_surname"]; 
         //} 
        } 

將對象添加到2D和1d陣列

if([tabbarData.ticketListViewMode isEqualToString:@"Maintenance"] || 
         [tabbarData.ticketListViewMode isEqualToString:@"Cleaning"] || 
         [tabbarData.ticketListViewMode isEqualToString:@"I.T."] || 
         [tabbarData.ticketListViewMode isEqualToString:@"viewAll"]){ 
         LoginData *login = [self dataObject]; 
         if([login.userRole isEqualToString:@"MR"] || [login.userRole isEqualToString:@"AS"] || 
          ([login.category isEqualToString:[temp objectForKey:@"category"]] && 
          ([login.userRole isEqualToString:@"CM"] || [login.userRole isEqualToString:@"UR"]))){ 
           [ticketList addObject:currentEntry]; 
           [ticketInformation addObject: currentEntry]; 
           [[multiArray objectAtIndex:0] addObject:currentEntry]; 
          } 
        } 
        else{ 
         [ticketList addObject:currentEntry]; 
         [ticketInformation addObject: currentEntry]; 
         [[multiArray objectAtIndex:0] addObject: currentEntry]; 
        } 
       } 

這種崩潰

NSLog(@"word: %@", [[[multiArray objectAtIndex:0] objectAtIndex:5] objectForKey:@"priority"]); 

這不會崩潰

NSLog(@"word1: %@", [[ticketList objectAtIndex:5] objectForKey:@"priority"]); 
+1

一個數組持有字典的數組。聽起來很OOP。向我們展示迄今爲止已嘗試過的內容 – crizzis

+0

已更新的問題 – apex

+1

請在添加到數組之前檢查currentEntry對象(如果爲null)。 – shuvo

回答

0

我覺得你要添加在multiArrayindex 1currentEntry對象,然後在index 0訪問它。

我跑你的代碼,它工作得很好:

NSArray *multiArray = [NSArray arrayWithObjects: 
           [NSMutableArray array], 
           [NSMutableArray array], 
           [NSMutableArray array], 
           [NSMutableArray array], 
           [NSMutableArray array],nil]; 

[[multiArray objectAtIndex:0] addObject:currentEntry]; 

NSLog(@"currentEntry Object: %@",[[multiArray objectAtIndex:0] objectAtIndex:0]); 

此外,還要確保currentEntry是不是增加了mutableArray前空。

希望它有幫助!

+0

[[multiArray objectAtIndex:0] addObject:currentEntry];我試圖像這樣添加它,仍然得到錯誤 – apex

+0

看看你在問題中更新的錯誤。看起來你正在訪問數組中的一個出界對象。也許你正在索引1訪問currentEntry而不是索引0. –

+0

當你在一個數組中添加一個對象時,默認情況下它會被添加到索引0。我打印出索引爲0的對象,它工作正常。 –