Reputation: 723
After so long, I'm still trying to create my first iOS app, and I'm facing another problem yet again =(
Basically, I have pulled JSON data from the web, and I get nested arrays. I have no problems iterating all objects in the array (including the parent array) and placing them in a UITableView
. The problem is, I am trying to do something dynamic for the table. I am able to create the correct number of sections for the table. To make it easy for you to visualise what I want to do, here is the JSON data:
{"filter_group":[{"filter_group_id":"1","name":"Location","filter":[{"filter_id":"2","name":"Central"},{"filter_id":"8","name":"East"},{"filter_id":"1","name":"North"},{"filter_id":"10","name":"Northeast"},{"filter_id":"9","name":"West"}]},{"filter_group_id":"3","name":"Price","filter":[{"filter_id":"7","name":"Free"},{"filter_id":"5","name":"$0 - $50"},{"filter_id":"6","name":"$50 - $100"},{"filter_id":"11","name":"$100 - $150"},{"filter_id":"12","name":"$150 - $200"},{"filter_id":"13","name":"Above $200"}]}]}
When you copy the messy chunk that I give you into any JSON viewer, you will see that for now, I have 2 parent arrays with some children each. So basically, "Location" and "Price" will go into different sections. I have successfully done that, but I don't know how to put the names of their children in the correct section of the UITableView
.
So my idea now is that I am iterating the array, and putting the children names into another array (this results in names of both sections going into 1 array). I was thinking of creating a new array in each iteration:
for(int i = 0; i < [myArray count]; i++) {
//throw children of different parents into different array
NSMutableArray* newArray[i] = (blah blah blah)
}
You will probably have seen the problem now: newArray[i]
. Basically, I can live with the fact that I am creating newArray0, newArray1 etc (I need my app to be dynamic). How do I do this in iOS programming?
OR
After reading what I want to do above, and you have a different idea on how I am to put the data in the respective sections, please enlighten me.
Thank you very much!!! I greatly appreciate any help offered =D
P.S. My replies may be slow, so please forgive me
Upvotes: 0
Views: 624
Reputation: 1731
In your application you can use array from json object. I've made sample project to illustrate this aproche:
In header file (ViewController.h):
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSMutableArray *sectionTitles;
@end
and in ViewController.m file:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *jsonString = @"{\"filter_group\":[{\"filter_group_id\":\"1\",\"name\":\"Location\",\"filter\":[{\"filter_id\":\"2\",\"name\":\"Central\"},{\"filter_id\":\"8\",\"name\":\"East\"},{\"filter_id\":\"1\",\"name\":\"North\"},{\"filter_id\":\"10\",\"name\":\"Northeast\"},{\"filter_id\":\"9\",\"name\":\"West\"}]},{\"filter_group_id\":\"3\",\"name\":\"Price\",\"filter\":[{\"filter_id\":\"7\",\"name\":\"Free\"},{\"filter_id\":\"5\",\"name\":\"$0 - $50\"},{\"filter_id\":\"6\",\"name\":\"$50 - $100\"},{\"filter_id\":\"11\",\"name\":\"$100 - $150\"},{\"filter_id\":\"12\",\"name\":\"$150 - $200\"},{\"filter_id\":\"13\",\"name\":\"Above $200\"}]}]}" ;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSArray *filter_groups = [result objectForKey:@"filter_group"];
self.data = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]];
self.sectionTitles = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]];
for (NSDictionary *filter_group in filter_groups) {
NSArray *filters = [filter_group objectForKey:@"filter"];
[self.data addObject:filters];
[self.sectionTitles addObject:[filter_group objectForKey:@"name"]];
}
NSLog(@"%@", self.data);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.sectionTitles[section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.data count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *sectionArray = self.data[section];
return [sectionArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"];
}
NSArray *sectionArray = self.data[indexPath.section];
NSDictionary *filter = sectionArray[indexPath.row];
cell.textLabel.text = [filter objectForKey:@"name"];
return cell;
}
@end
If you have same question to code, feel free to ask. You can grab code from https://gist.github.com/MaciejGad/8452791
Upvotes: 1
Reputation: 619
please try the following code.
NSMutableDictionary *responseDict; // assign value to responseDict from your response
NSMutableArray *mainArray=[[NSMutableArray alloc] initWithArray:[responseDict objectForKey:@"filter_group"]];
for(int i=0;i<[mainArray count];i++)
{
if([[[mainArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:@"Price"])
{
NSArray *pricechildArray=[[mainArray objectAtIndex:i] objectForKey:@"filter"];
}
else if([[[mainArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:@"Location"])
{
NSArray *locationchildArray=[[mainArray objectAtIndex:i] objectForKey:@"filter"];
}
}
hope this will help you.
Upvotes: 0
Reputation: 652
create a new temp array in each loop and add that new temp array to a Mutable array, it is similar to new array[i], while retrieving NSArray *array = [mutablearray objectAtIndex:i]`
Upvotes: 0