2016-09-23 103 views
0

我試圖將NSDictionary數據放入表視圖中。但是我遇到問題存在價值。將NSDictionary轉換爲UITableView

字典看起來像這樣:使用的XMLReader

multimedia =  { 
     uploads =   { 
      upload =    (
           { 
        FileName =      { 
         text = "\n  1-FB5A9792.MOV"; 
        }; 
        status =      { 
         text = "\n  2"; 
        }; 
        Event =      { 
         text = "\n  Test"; 
        }; 
        Date =      { 
         text = "\n  9/7/2016 9:06:21 AM"; 
        }; 
        Publicacion =      { 
         text = "\n  Example"; 
        }; 
        Seccion =      { 
         text = "\n  Sistemas"; 
        }; 
        id =      { 
         text = "\n \n \n  993"; 
        }; 
        text = "\n "; 
       }, 
           { 
        FileName =      { 
         text = "\n  2-FB5A9793.MOV"; 
        }; 
        status =      { 
         text = "\n  2"; 
        }; 
        Event =      { 
         text = "\n  Test"; 
        }; 
        Date =      { 
         text = "\n  9/7/2016 9:06:21 AM"; 
        }; 
        Publicacion =      { 
         text = "\n  Example"; 
        }; 
        Seccion =      { 
         text = "\n  Sistemas"; 
        }; 
        id =      { 
         text = "\n \n  994"; 
        }; 
        text = "\n "; 
       }, 
           { 
        FileName =      { 
         text = "\n  1-IMG_0006.MOV"; 
        }; 
        status =      { 
         text = "\n  2"; 
        }; 
        Event =      { 
         text = "\n  Highest"; 
        }; 
        Date =      { 
         text = "\n  9/7/2016 4:56:37 PM"; 
        }; 
        Publicacion =      { 
         text = "\n  Example"; 
        }; 
        Seccion =      { 
         text = "\n  Sistemas"; 
        }; 
        id =      { 
         text = "\n \n  995"; 
        }; 
        text = "\n "; 
       },... 

I'm轉換的XML到詞典,則上述數據傳遞到資料表

tableData = [jsonDictionary allKeys]; 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

創建單元

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    NSString *key = [tableData objectAtIndex:indexPath.row]; 
    NSDictionary *dictionary = [jsonDictionary objectForKey:key]; 
    // get values from the dictionary and set the values for the displayed cell ... 

    return cell; 
} 

我試圖爲每個FileName和狀態值放置一個單元格,在這種情況下爲每個FileName放置3個單元格。 但我不知道如何做到這一點或獲得FileName的數量。

這是XML

<multimedia> 
    <uploads> 
    <upload> 
     <id>1136</id> 
     <Date>9/9/2016 11:01:58 AM</Date> 
     <FileName>IMG_0510.MOV</FileName> 
     <status>2</status> 
     <Publicacion>Example</Publicacion> 
     <Seccion>Sistemas</Seccion> 
     <Event>Q</Event> 
    </upload> 
    </uploads> 
</multimedia> 
+0

是那個'jsonDictionary'的輸出?或者顯示分配給'jsonDictionary'的值。 –

+0

是的,是jsonDictionary日誌,'NSLog(@「%@」,jsonDictionary);' – CGR

+0

''jsonDictionary.count'輸出什麼' –

回答

0

獲取的數據是這樣的:NSMutableArray *tableData = dict[@"multimedia"][@"uploads"][@"upload"];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    NSDictionary *upload = [tableData objectAtIndex:indexPath.row]; 
    // get values from the dictionary and set the values for the displayed cell ... 

    NSString *event = upload[@"Event"][@"text"]; 
    event = [event stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    cell.labelEvent.text = event; 

    NSString *date = upload[@"Date"][@"text"]; 
    date = [date stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    cell.labelDate.text = date; 

    return cell; 
} 
+0

'upload'從'FileName = {...'打印直到'text =「\ n」;'在每個文件中,''上傳[@「Event」] [@「text」];'什麼都不打印,標籤是空:( – CGR

+1

由於UB先生,我改變 '[dictInProgress的setObject:textInProgress forKey:kXMLReaderTextNodeKey];'由 '[dictInProgress的setObject:[textInProgress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] forKey:kXMLReaderTextNodeKey];'在XMLReader.m並顯示數據 – CGR

1

它看起來對我說,計數應該是1

原始jsonDictionary只有一個項目。你應該更深入的上傳,然後調用計數。

@"upload"保存數組裏面。所以,jsonDictionary[@"multimedia"][@"uploads"][@"upload"]應返回array。如果您將其轉換爲NSDictionary,它將返回錯誤的結果,並且如果您嘗試獲得關鍵值File name的值,則會得到錯誤的結果。

字典的主要目的是你只有一個特定的鍵的值。這就是爲什麼你只能得到一個File name,如果你把數組放入NSDictionary

所以:

NSArray * array = _xmlDictionary[@"multimedia"][@"uploads"][@"upload"]; 
NSDictionary * onUpload = array[i]; 
NSDictionary * fileName = array[i][@"FileName"]; 
NSString * fileNameString = array[i][@"FileName"][@"text"]; 
你的情況

所以:

tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"]; 

然後

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    NSDictionary *upload = [tableData objectAtIndex:indexPath.row]; 
    NSString *fileName = upload[@"FileName"][@"text"]; 
    NSString *status = upload[@"status"][@"text"]; 
    NSString *event = upload[@"event"][@"text"]; 
    ... 

    return cell; 
} 
+0

'NSDictionary * subDictionary = jsonDictionary [@「multimedia」] [@「uploads」] [@「upload」]; ' 'NSUInteger keyCount = [subDictionary count]; //返回8'只有一個'FileName',不知道爲什麼 – CGR

+0

我更新了我的回答 –

+0

'tableData [0] [@「FileName」];'當我只有一個'FileName'時崩潰一個它給出的值,但將代碼作爲'cellForRowAtIndexPath'中的標籤文本放置並崩潰並記錄'終止於類型爲NSException的未捕獲異常 – CGR

0

我已將您的JSON放入本地JSON文件並進行解析,以顯示結果。

所以,你可以按照得到的字典後的代碼,

因此,行,你應該從這個線
NSArray *json = jsonTemp[@"multimedia"][@"uploads"][@"upload"];

jsonTemp包含你的字典中的所有數據開始。 這裏在代碼中我只顯示文件名,您可以使用其他鍵來相應地顯示其他數據。

這裏是我的代碼:

- (void)loadJsonData 
{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"]; 
    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath]; 

    NSDictionary *jsonTemp = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; 

    NSArray *json = jsonTemp[@"multimedia"][@"uploads"][@"upload"]; 

    for (NSDictionary *dictTemp in json) { 
     [arrEvents addObject:dictTemp]; 
    } 

    [tblView reloadData]; 
} 

#pragma mark - TableView Delegate - 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [arrEvents count]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 49; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"ListingIdentifier"; 

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier]; 
    } 

    NSDictionary *dictTemp = [arrEvents objectAtIndex:indexPath.row]; 
    NSString *fileName = dictTemp[@"FileName"][@"text"]; 

    cell.textLabel.text = [NSString stringWithFormat:@"File Name : %@", fileName]; 

    cell.backgroundColor = CLEAR_COLOR; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 

輸出: enter image description here

希望它可以幫助

快樂編碼...