1

このJSONをセクション化されたUITableに解析する方法を何日も見つけようとしてきましたが、うまくいきません.セクション名を取得する方法しかわかりませんでしたが、各セクションの行数とデータを取得できませんでした各セクションの各行。

輸送グループは時々変化し、その名前も変わる可能性があるため、allKeys を使用して各セクションのタイトルを 1 番目に見つける必要があると思います。

セクション化された 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];
}
4

3 に答える 3

2

必要な作業の鍵は、JSON 文字列がオブジェクトに解析されると、一連のネストされた NSArray と NSDictionary になることを認識することです。値を適切に掘り下げるだけで済みます。

- (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;
}
于 2012-01-27T09:36:06.840 に答える
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;
 }

とにかくそれが要点です。

allKeysすべてのテーブルビューのデリゲートおよびデータソース メソッドでそれらを処理する代わりに、配列と配列をクラス プロパティとして格納することallValuesをお勧めしますが、今すぐ yuor テーブルを構築するためのすべての情報が必要です。

お役に立てれば :)

于 2012-01-27T09:38:32.837 に答える
1
NSDictionary *results = [jsonString JSONValue];
NSDictionary *allTypes = [results objectForKey:@"transport"];
NSArray *allTransportKeys = [allTypes allKeys];

セクション数:

NSInteger numberOfSections = [allKeys count];

セクションの行数:

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

indexPath のデータ:

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

その後、itemDict からデータを抽出できます。

于 2012-01-27T09:25:10.773 に答える