0

Building an iOS app which is parsing key "ingredients" out of a local JSON array then displaying the "ingredients" values in a tableview. The problem is each ingredients key has multiple values. Each value needs to be separated into their own tableview cells and I can't figure out how to accomplish. My knowledge is very limited and I've only been able to display the combined ingredients values into the tableview cells.

How do I separate the values so that each value in ingredients has its own tableview cell?

Heres just 2 examples of data in the JSON array:

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

Here's the tableview code:

#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

Here's the Location.h file

#import <Foundation/Foundation.h>

@interface Location : NSObject

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



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



@end

and the Location.m file

#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
Jason
  • 15
  • 5
  • How is `Location` declared? Basically you need an array `ingredients` and the method `componentsSeparatedByString` to separate the values into an array. Then use the `Location` object as `section` and `ingredients` as `rows` in the table view. – vadian Jul 15 '16 at 04:12
  • json data is not valid so i am edit it – HariKrishnan.P Jul 15 '16 at 05:40
  • Thanks for the replies. Im unable to get those solutions to work as they all throw errors...too many to list. @vadian I'm adding the location code. Can you provide an example of your suggestion in code? It would be greatly appreciated. Im still learning. – Jason Jul 15 '16 at 23:57

2 Answers2

0

Try this one :--

 NSString *str_Calories_List = [[responseObject valueForKeyPath:@"location.name"] componentsJoinedByString:@", "];
user6438311
  • 117
  • 1
  • 3
  • 13
Birendra
  • 623
  • 1
  • 5
  • 17
0

I have tried this which using your json data

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;
   }
HariKrishnan.P
  • 1,204
  • 13
  • 23
  • Thanks for the reply, but do you have an example that would read from my local json file as the my current code does? Theres thousands of these recipes and entering them all in the tableview controller as you have will not be feasable. – Jason Jul 16 '16 at 00:17
  • Refer this link.http://stackoverflow.com/questions/22528800/ios-how-to-make-the-tableview-use-paginated-api-output Paginator using to load the 1000 of data.can you use the same dictionary to load the data in tableview – HariKrishnan.P Jul 16 '16 at 14:22