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
を使用している 'Location'が宣言されている方法は?基本的に配列 'ingredients'と' componentsSeparatedByString'メソッドが必要です。値を配列に区切ります。そして 'Location'オブジェクトをテーブルビューの'行 'として' section'と 'ingredients'として使います。 – vadian
jsonデータが有効でないので編集しています –
返信いただきありがとうございます。イムは、すべてのエラーをスローするようにそれらのソリューションを動作させることができません...あまりにも多くのリストに。 @vadian私は場所コードを追加しています。あなたの提案の例をコードで提供できますか?大変感謝しています。まだ勉強してる。 – Jason