2012-01-27 22 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