构建一个iOS应用程序,它从本地JSON数组中解析关键“成分”,然后在tableview中显示“成分”值。问题是每个成分键都有多个值。每个值都需要分成他们自己的tableview单元格,我无法弄清楚如何完成。我的知识非常有限,我只能将组合成分值显示在tableview单元格中。
如何分离值,以便成分中的每个值都有自己的tableview单元格?
仅有2个JSON数组中的数据示例:
{
"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"}
]
}
这是tableview代码:
#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
和Location.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 :(得分:0)
试试这个: -
NSString *str_Calories_List = [[responseObject valueForKeyPath:@"location.name"] componentsJoinedByString:@", "];
答案 1 :(得分: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;
}