Skip to content

Instantly share code, notes, and snippets.

@MaciejGad
Created January 16, 2014 10:32
Show Gist options
  • Save MaciejGad/8452791 to your computer and use it in GitHub Desktop.
Save MaciejGad/8452791 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSMutableArray *sectionTitles;
@end
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment