4

here is my data pattern and i want to create an array of sections, in which case, each section will include an array of rows in that section. I looked at the following questions:

and a few other posts but could get no result. Can anyone please guide me how to create an array of sections, and each section shall include its own rows.. Thanks in advance

EDIT I have allready parsed my data, the log result at the pastebin link is an NSDictionary here is how i fetch&parse it:

 NSString*key1=[ result objectForKey:@"key" ];                
         NSString *s1=[@"http://" stringByAppendingString:server.text];
         NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
         NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];

        NSData *data2=[NSData dataWithContentsOfURL:url2];
         result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];

The result in pastebin is this: NSLog(@"Result: %@", result2);

5
  • @Jack, i followed your answer but couldnt get any result, can u give some more help please? Commented Jun 4, 2012 at 8:19
  • I didn't give any answer, I only reformatted your question Commented Jun 4, 2012 at 8:19
  • ah sorry, the third answer to this one has been given by someone called Jack. i tought he is you:) Commented Jun 4, 2012 at 8:22
  • Is result2 a valid dictionary object? Commented Jun 4, 2012 at 11:27
  • result2 contains two main variables, one called version, which is a single number, and an array of arrays(the elements of inner array are dictionaryies..).. it is something like this; buttonVersion:(some_number), template{Category1:(obj1,obj2...);Category2:( obj1, obj2...)...} i hope the scenerio is clear enough Commented Jun 4, 2012 at 11:33

1 Answer 1

1

Your data is in JSON format, so just use the NSJSONSerialization class to create the array of arrays for you.

NSString *yourData;
NSData *data = [yourData dataUsingEncoding:NSUTF8StringEncoding];
NSArray *arrayOfArrays = [NSJSONSerialization JSONObjectWithData:data 
    options:NSJSONReadingAllowFragments error:nil];

To use your array of sections in a table view, just use the usual table view datasource methods:

// numberOfSectionsInTableView
return [array count];

// numberOfRowsInSection
return [[array objectAtIndex:section] count];

// cellForRowAtIndexPath
cell.textLabel.text = [[[array objectAtIndex:indexPath.section] 
   objectAtIndex:indexPath.row]
      objectForKey:"name"];
Sign up to request clarification or add additional context in comments.

13 Comments

i have already parsed data with NSJSONSerialization, but i shall try your code
Then use an error variable NSError *error; like this ...error:&error]; NSLog(@"%@", error); and see what it says.
Then please log what your string, your data, your array and your error is - the mistake will become apparent.
please see my edit, i have allready used NSJSONSerialization before. what can i do from this point?
Congratulation, you have solved the problem. Now all you need to do is to check the check mark... For using the sections and rows in the tableview, see my edit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.