Section UITable& JSON [英] Sectioned UITable & JSON

查看:146
本文介绍了Section UITable& JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几天来弄清楚如何将这个JSON解析为一个UITable,但是我没有成功,我只能弄清楚如何获取节名,但未能得到每个节的行每个部分中每行的计数和数据。

I have been trying for days to figure out how to parse this JSON to a sectioned UITable but I am not successful, I've only been able to figure out how to get the section name, but failed to get each section row count and data for each row in each section.

由于交通运输组可能会不时变化,而且他们的名字可能会有所变化,所以我想我需要使用allKeys查找每个部分标题第1名。

Since the transportation group may vary from time to time and their name may change, so I guess I need to use allKeys to find out each section title 1st.

请帮助并指出正确的方向来提取分段UITable的数据,谢谢。

Please help and points me to the right direction to extract the data for a sectioned UITable, Thank you.

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


推荐答案

最简单的解决方案是使用所有字典作为数据源。

The easiest solution to explain is to use the all dictionary as your datasource.

NSDictionary *results = [jsonString JSONValue];

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

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

然后获取您可以执行的部分数量:

Then to get the number of sections you could do :

- (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;
 }

无论如何,这都是它的主旨。

That's the gist of it anyway.

您可能希望将 allKeys allValues 数组存储为类属性,而不必去通过它们在所有tableview的委托和数据源方法中,但你应该拥有现在构建yuor表的所有信息。

You might want to store the allKeys and allValues arrays as class properties instead of having to go through them in all the tableview's delegate and datasource methods, but you should have all the info to build yuor table now.

希望这会有所帮助:)

这篇关于Section UITable& JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆