1

In fact, I am new in iOS programming, and I am trying to learn it by myself.

I have a sectioned table view, with header sections, but I want the view to be able to collapse expand the rows in section when this section is tapped.

I use this tutorial: http://blog.paxcel.net/blog/expandablecollapsible-table-for-ios/

But In my application, I have a Json File and not a Plist file.

So I use in setCategoryArray() function:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
                [NSURL URLWithString: @"http://......./catjsonf.php"]];

NSError* error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

jsonResults = [json objectForKey:@"nodes"];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});


});

instead of :

  NSURL *url = [[NSBundle mainBundle] URLForResource:@”CategoryList” withExtension:@”plist”];

NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];

But it doesn't work - should I do something else?

1
  • You are taking array from webservice. It will take time. Use asynchronous method and reload your table in connectionDidFinishLoading.
    – Baby Groot
    Apr 2, 2013 at 9:43

1 Answer 1

1

check out my blog it will surely help you. here is the link.

Expandable/Collapsible Table For iOS

you need to do the following things

  1. First design your classes according to your json file as in my case it is "Category".
  2. Once you are done with model classes you need to edit the following method to prepare array of model objects.

Hint: From json, we can directly find out our dictionary like

id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

where result can further casted to get the dictinary object. And the load data simply like the following method

    - (void) setCategoryArray
    {

          NSURL *url = [[NSBundle mainBundle] URLForResource:@”CategoryList” withExtension:@”plist”];

          NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];

          NSMutableArray *categoryArray = [[NSMutableArray alloc] initWithCapacity:[mainArray count]];

         for (NSDictionary *dictionary in mainArray) 
         {

              Category *category = [[Category alloc] init];

              category.name = [dictionary objectForKey:@"name"];

              category.list = [dictionary objectForKey:@"list"];

              [categoryArray addObject:category];

          }

         self.categoryList = categoryArray;
     }
  1. Once this done, the only thing left to edit the table view's data source and delegate methods accordingly.
9
  • I've built my application based on this code, but the problem that I have a Json file instead of plist file. Could you help me, how can I implement it please.
    – CarinaM
    Apr 2, 2013 at 9:45
  • :P...you used my blog earlier i didn't noticed that. Thats great. Thanks. Will be back to you with solution. Please give me some time. Apr 2, 2013 at 10:01
  • It doesnot matter what is your data source. Once you get your Model array ready. You load the table on that bases. In My actual application, i have loaded the data from json only. Apr 2, 2013 at 10:04
  • I am sure, you will help me, because I like your work and you will resolve my problem
    – CarinaM
    Apr 2, 2013 at 11:56
  • I am waiting for your support :)
    – CarinaM
    Apr 2, 2013 at 13:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.