问题描述:

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?

网友答案:

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.
相关阅读:
Top