0

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

6
  • What's even NSArray* newArray[i] = ...? That code creates a variable-length raw C array of pointers (which point to NSArray instances). And you can't even initialize it because it's forbidden. Are you trying to create an array of arrays? If so, you can use NSMutableArray and fill it in... but this sounds kind of trivial. You need to learn the language before trying to do clever things (and this means that you absolutely need to be proficient in C for writing iOS apps). Commented Jan 16, 2014 at 9:15
  • what is the problem of using nested arrays... just create a new temporary array and add that array as an object to NSMutableArray Commented Jan 16, 2014 at 9:17
  • @H2CO3 I know it is forbidden. It sounds trivial, but I haven't been able to find a solution thus far. I have basic knowledge on programming, and from my experience, there is no point in learning the language totally. You can't apply. The only way to learn is to do it ;) Commented Jan 16, 2014 at 9:20
  • @BalaChandra I am able to do that, but how do I separate the 2 children arrays? It seems simple, but I just don't see how to do it =/ Commented Jan 16, 2014 at 9:38
  • your problem is how to create a new array in each loop right.. what i am telling you is 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] Commented Jan 16, 2014 at 9:44

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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]`

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.