Reputation: 123
I call a webservice and get a nice JSON in return. This JSON lists a couple of reports with category.
The big question is how I can make a nice looking tableview with this, grouped by category. Im new to iOS, and I am really stucked at this point.
I save the json in an array like this:
tableData = [NSJSONSerialization JSONObjectWithData:dataWebService options:kNilOptions error:&error];
And then I sort the list:
NSArray *sortedArray;
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Category" ascending:YES];
sortedArray = [tableData sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
The json I get is this:
{
Category = Faktura;
about = "Fakturablankett med giro med utvalg p\U00e5 fra-til fakturanr";
name = Faktura;
reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
reportid = 16;
},
{
Category = Faktura;
about = "Fakturablankett med giro med utvalg p\U00e5 fra-til fakturanr";
name = "Faktura med sidenummer";
reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
reportid = 19;
},
{
Category = Faktura;
about = "Liste over fakturaer med status og mva-detaljer. Utvalg p\U00e5 fra-til fakturanr.";
name = Fakturajournal;
reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
reportid = 15;
},
{
Category = "Journaler og Kontoutskrifter";
about = "";
name = "Kontoutskrift hovedbok";
reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
reportid = 4;
},
{
Category = "Journaler og Kontoutskrifter";
about = "";
name = "Kontoutskrift kunder";
reportendpoint = "https://unionline.unimicro.no/uni24Report/Report.aspx";
reportid = 5;
}
I would like to list these "name" in a tableview, grouped by "Category". I need to sort the category an list the reports who belongs to these categories.
There is many more categories, but I didn´t paste them all.
Upvotes: 1
Views: 2665
Reputation: 5835
You have to create one global array which will has list of category arrays.
To create global array with elements called category-arrays, used didReceiveResponseJson:
and nameDitionaryAllReadyExist:
methods.
To view above json data in UITableView:
NSMutableArray* tableArray;//Declare this array globally and allocate memory in viewdidload
-(NSMutableArray *)nameDitionaryAllReadyExist:(NSString *)name {
for(NSMutableArray *nameArray in tableArray){
for(NSDictionary* nameDict in nameArray) {
if([[nameDict objectForKey:@"Category"] isEqualToString:name])
//return the existing array refrence to add
return nameArray;
}
}
// if we dont found then we will come here and return nil
return nil;
}
-(void)didReceiveResponseJson:(NSArray *)jsonArray {
for(NSDictionary* nameDict in jsonArray) {
NSMutableArray* existingNameArray=[self nameDitionaryAllReadyExist:[nameDict objectForKey:@"Category"]];
if(existingNameArray!=nil) {
//if name exist add in existing array....
[existingNameArray addObject:nameDict];
}
else {
// create new name array
NSMutableArray* newNameArray=[[[NSMutableArray alloc] init] autorelease];
// Add name dictionary in it
[newNameArray addObject:nameDict];
// add this newly created nameArray in globalNameArray
[tableArray addObject:newNameArray];
}
}
//so at the end print global array you will get dynamic array with the there respetive dict.
NSLog(@"Table array %@", tableArray);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableArray=[[NSMutableArray alloc] init];
NSString* path=[[NSBundle mainBundle] pathForResource:@"JsonData" ofType:@"json"];
NSDictionary* tableData=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:kNilOptions error:nil];
//NSLog(@"Table Array- %@",tableData);
NSArray* dataArray = [tableData objectForKey:@"data"];
[self didReceiveResponseJson:dataArray];
}
#pragma mark
#pragma mark UITableViewDataSource
//@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray* array=[tableArray objectAtIndex:section];
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray* array=[tableArray objectAtIndex:indexPath.section];
NSDictionary* item=[array objectAtIndex:indexPath.row];
NSString* name=[item valueForKey:@"name"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil) {
// No cell to reuse => create a new one
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
// Initialize cell
}
// Customize cell
cell.textLabel.text = name;
return cell;
}
//@optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [tableArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray* array=[tableArray objectAtIndex:section];
if([array count]) {
NSDictionary* item=[array objectAtIndex:0];
return [item valueForKey:@"Category"];
}
else
return nil;
}
Upvotes: 4