2016-07-15 7 views
0

iOSアプリケーションをビルドします。ローカルのJSON配列からキー「成分」を解析し、「成分」値をテーブルビューに表示します。問題は、各成分キーが複数の値を持つことです。各値は、独自のテーブルビューセルに分割する必要があり、どのように達成するのかわかりません。私の知識は非常に限定されており、結合された成分の値をtableviewセルに表示することしかできませんでした。1複数の値がそれぞれuitableviewセルに分割されたJSONキー

成分の各値に独自のテーブルビューセルがあるように値を分割するにはどうすればよいですか?

HERESにJSON配列内のデータのわずか2例:ここでは

#import "FilterViewController.h" 
#import "LocationsViewController.h" 
#import "Location.h" 
#import "JSONLoader.h" 

@implementation FilterViewController { 
NSArray *_locations; 


} 



- (void)viewDidLoad { 
[super viewDidLoad]; 

// Create a new JSONLoader with a local file URL 
JSONLoader *jsonLoader = [[JSONLoader alloc] init]; 
NSURL *url = [[NSBundle mainBundle] URLForResource:@"locations" withExtension:@"json"]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    _locations = [jsonLoader locationsFromJSONFile:url]; 

    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 


}); 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
LocationsViewController *vc = segue.destinationViewController; 
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
vc.location = [_locations objectAtIndex:indexPath.row]; 
} 

#pragma mark - Table View Controller Methods 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FilterCell"]; 

Location *location = [_locations objectAtIndex:indexPath.row 

cell.textLabel.text = location.ingredients; 

cell.imageView.image = [UIImage imageNamed:@"ingredientsicon3232.png"]; 

return cell; 
} 

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

@end 

Location.hが

#import <Foundation/Foundation.h> 

@interface Location : NSObject 

- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary; 



@property (readonly) NSString *name; 
@property (readonly) NSString *ingredients; 
@property (readonly) NSString *cookTime; 



@end 

と場所を、ファイルの:

{ 
    "locations": [ 


{"name" : "Drop Biscuits and Sausage Gravy", "ingredients" :  "Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n1/2 teaspoon Salt\n1-1/2 stick (3/4 Cup) Cold Butter, Cut Into Pieces\n1-1/4 cup Butermilk\n SAUSAGE GRAVY\n1 pound Breakfast Sausage, Hot Or Mild\n1/3 cup All-purpose Flour\n4 cups Whole Milk\n1/2 teaspoon Seasoned Salt\n2 teaspoons Black Pepper, More To Taste", "cookTime" : "PT30M"}, 

{"name" : "Hot Roast Beef Sandwiches", "ingredients" : "12 whole Dinner Rolls Or Small Sandwich Buns (I Used Whole Wheat)\n1 pound Thinly Shaved Roast Beef Or Ham (or Both!)\n1 pound Cheese (Provolone, Swiss, Mozzarella, Even Cheez Whiz!)\n1/4 cup Mayonnaise\n3 Tablespoons Grated Onion (or 1 Tbsp Dried Onion Flakes))\n1 Tablespoon Poppy Seeds\n1 Tablespoon Spicy Mustard\n1 Tablespoon Horseradish Mayo Or Straight Prepared Horseradish\n Dash Of Worcestershire, "cookTime" : "PT20M"} 
    ] 
} 

は、ここでテーブルビューのコードです.mファイル

#import "Location.h" 

@implementation Location 

// Init the object with information from a dictionary 
- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary { 
    if(self = [self init]) { 
     // Assign all properties with keyed values from the dictionary 


     _name = [jsonDictionary objectForKey:@"name"]; 
     _ingredients = [jsonDictionary objectForKey:@"ingredients"]; 
     _cookTime = [jsonDictionary objectForKey:@"cookTime"]; 

    } 

    return self; 
} 

@end 
+0

を使用している 'Location'が宣言されている方法は?基本的に配列 'ingredients'と' componentsSeparatedByString'メソッドが必要です。値を配列に区切ります。そして 'Location'オブジェクトをテーブルビューの'行 'として' section'と 'ingredients'として使います。 – vadian

+0

jsonデータが有効でないので編集しています –

+0

返信いただきありがとうございます。イムは、すべてのエラーをスローするようにそれらのソリューションを動作させることができません...あまりにも多くのリストに。 @vadian私は場所コードを追加しています。あなたの提案の例をコードで提供できますか?大変感謝しています。まだ勉強してる。 – Jason

答えて

0

、このいずれかを試してください: -

NSString *str_Calories_List = [[responseObject valueForKeyPath:@"location.name"] componentsJoinedByString:@", "]; 
0

私はこれを試してみました、あなたのJSONデータ

NSDictionary *setObject = @{ 
     @"name": @"Drop Biscuits and Sausage Gravy", 
     @"ingredients": @"Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n1/2 teaspoon Salt\n1-1/2 stick (3/4 Cup) Cold Butter, Cut Into Pieces\n1-1/4 cup Butermilk\n SAUSAGE GRAVY\n1 pound Breakfast Sausage, Hot Or Mild\n1/3 cup All-purpose Flour\n4 cups Whole Milk\n1/2 teaspoon Seasoned Salt\n2 teaspoons Black Pepper, More To Taste", 
     @"cookTime": @"PT30M" 
     }; 


    NSArray *getArrayRespose =[NSArray arrayWithObjects:setObject,setObject, nil]; 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init]; 
    [dic setObject:getArrayRespose forKey:@"locations"]; 
    NSLog(@"%@",dic); 

    //I am tested to separate the string like below 

    NSArray *getObject = [dic objectForKey:@"locations"]; 
    NSDictionary *getFirstObject = [getObject objectAtIndex:0]; 
    NSString *name = [getFirstObject objectForKey:@"name"]; 
    NSString *ingred = [getFirstObject objectForKey:@"ingredients"]; 
    NSString *cook = [getFirstObject objectForKey:@"cookTime"]; 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    NSArray *getObject = [dic objectForKey:@"locations"]; 
    return [getObject count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    // here you create the tableviewCell 
    NSArray *getObject = [dic objectForKey:@"locations"]; 
    NSDictionary *getFirstObject = [getObject objectAtIndex:indexPath.row]; 
    NSString *name = [getFirstObject objectForKey:@"name"]; 
    NSString *ingred = [getFirstObject objectForKey:@"ingredients"]; 
    NSString *cook = [getFirstObject objectForKey:@"cookTime"]; 

    // can you use the separated string in your cell 

    cell.titleLabel.text = name; 
    } 
+0

返信いただきありがとうございますが、私の現在のコードが行っているようにローカルjsonファイルから読み込む例がありますか?これらのレシピは何千もあり、あなたが持っているようにそれらをすべてtableviewコントローラに入力することは実現可能ではありません。 – Jason

+0

このリンクを参照してください.http://stackoverflow.com/questions/22528800/ios-how-to-make-the-tableview-use-paginated-api-output Paginatorを使用して1000のデータを読み込みます。同じものを使用できます。テーブルビューでデータを読み込む辞書 –

関連する問題