2012-01-27 27 views
1

我一直在想幾天來弄清楚如何解析這個JSON到一個分段的UITable,但我不成功,我只能弄清楚如何獲得節名稱,但未能獲取每個部分中每一行的行數和數據。分段的UITable&JSON

由於交通團隊可能會不時變化,他們的名字可能會改變,所以我想我需要使用allKeys來找出每節的標題1st。

請幫助並指出我正確的方向來提取分區UITable的數據,謝謝。

{ 
    "transport" : { 
    "public" : [ 
    { 
     "transport_id" : "2", 
     "transport_name" : "Ferry" 
    }, 
    { 
     "transport_id" : "3", 
     "transport_name" : "Bus" 
    }, 
    { 
     "transport_id" : "4", 
     "transport_name" : "Taxi" 
    }, 
    { 
     "transport_id" : "5", 
     "transport_name" : "Tram" 
    } 
    ], 
    "Private" : [ 
    { 
     "transport_id" : "11", 
     "transport_name" : "Bicycle" 
    }, 
    { 
     "transport_id" : "12", 
     "transport_name" : "Private Car" 
    } 
    ], 
    "Misc" : [ 
    { 
     "transport_id" : "6", 
     "transport_name" : "By Foot" 
    }, 
    { 
     "transport_id" : "7", 
     "transport_name" : "Helicopter" 
    }, 
    { 
     "transport_id" : "8", 
     "transport_name" : "Yatch" 
    } 
    ] 
    } 
} 



NSDictionary *results = [jsonString JSONValue]; 

NSDictionary *all = [results objectForKey:@"transport"]; 
NSArray *allKeys = [all allKeys]; 

NSArray *transports = [results objectForKey:@"transport"]; 
for (NSDictionary *transport in transports) 
{ 
    [transportSectionTitle addObject:(transport)]; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return [transportSectionTitle count]; 
} 
+0

你想說,運輸,公私錯過這些改變或在其內部的數據在一段時間內發生變化。 – 2012-01-27 08:33:39

回答

2

最簡單的解決方案是使用all字典作爲數據源。

NSDictionary *results = [jsonString JSONValue]; 

NSDictionary *all = [results objectForKey:@"transport"]; 

// self.datasource would be a NSDictionary retained property 
self.datasource = all; 

然後讓你可以做部分的數量:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [self.datasource count]; // You can use count on a NSDictionary 
} 

要獲得部分的標題:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    NSString *title = [[self.datasource allKeys] objectAtIndex:section]; 

    return title; 
} 

要獲得每個部分的行數:

- (NSInteger)tableView:(UITableView *)favTableView numberOfRowsInSection:(NSInteger)section { 

    // Get the all the transports 
    NSArray *allTransports = [self.datasource allValues]; 

    // Get the array of transports for the wanted section 
    NSArray *sectionTransports = [allTransports objectAtIndex:section]; 

    return [sectionTransports count]; 
} 

然後得到行:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Get the all the transports 
    NSArray *allTransports = [self.datasource allValues]; 

    // Get the array of transports for the wanted section 
    NSArray *sectionTransports = [allTransports objectAtIndex:indexPath.section]; 

    // Then get the transport for the row 
    NSDictionary *transport = [sectionTransports objectAtIndex:indexPath.row]; 

    // Now you can get the name and id of the transport 
    NSString *tansportName = [transport objectForKey:@"transport_name"]; 
    NSString *transportId = [transport objectForKey:@"transport_id"]; 

    NSString *transportDescription = [NSString stringWithFormat:@"%@ - %@",transportId, transportName]; 

    cell.textLabel.text = transportDescription; 

    return cell; 
} 

無論如何這就是它的要義。

您可能想要將allKeysallValues數組存儲爲類屬性,而不是必須在所有tableview的委託和數據源方法中遍歷它們,但是您應該有所有信息來構建yuor表。

希望這有助於:)

+0

謝謝你,你的答案是非常詳細的,它可以幫助我很多。通常,我將在接收到JSON後將所有值分配給NSMutableArray ... – 2012-02-05 10:21:51

1
NSDictionary *results = [jsonString JSONValue]; 
NSDictionary *allTypes = [results objectForKey:@"transport"]; 
NSArray *allTransportKeys = [allTypes allKeys]; 

數量部分組成:

在indexPath
NSString *key = [allKeys objectAtIndex:section]; 
NSArray *array = [allTypes objectForKey:key]; 
NSInteger numberOfRows = [array count]; 

數據:

NSString *key = [allKeys objectAtIndex:indexPath.section]; 
NSArray *array = [allTypes objectForKey:key]; 
NSDictionary *itemDict = [array objectAtIndex:indexPath.row]; 
NSInteger numberOfSections = [allKeys count]; 

在部中的行數然後你可以從itemDict中提取數據。

2

的關鍵,你需要做的是承認,一旦你的JSON字符串被解析成一個對象就變成了一系列的嵌套NSArrays和NSDictionarys的,你只需要通過值適當鑽

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[transports allKeys] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [(NSArray*)[transports objectForKey:[[transports allKeys] objectAtIndex:section]] count]; 
} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return [transports allKeys]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // get transport category (e.g."public") 
    NSString *transportCategory = (NSString*)[[transports allKeys] objectAtIndex:[indexPath section]]; 

    // get transport items belonging to the category 
    NSArray *items = (NSArray*)[transports objectForKey:transportCategory]; 

    // get transport item for this row 
    NSDictionary *transportItem = [items objectAtIndex:[indexPath row]]; 

    // extract values of transport item 
    NSString *transportName = [transportItem objectForKey:@"transport_name"]; 
    NSString *transportID = [transportItem objectForKey:@"transport_id"]; 

    cell.textLabel.text = transportName; 

    return cell; 
} 
+0

感謝您的回答,它也有幫助,但Mutix的答案更完整,所以我標記爲我的選擇=) – 2012-02-05 10:23:14