I've a requirement of displaying data from web service in an expandable table view up to 2 levels. While this is possible with static data, I am unable to find a way to make this work with data retrieved from backend. An example of this in Postman is shown below:
One Level UITableView Data in Postman
The above is for first-level expansion only. Here, "Accountants" is the main category and "jj tags", "Certified Public" & "General Services" are the sub categories that would be visible after expansion (once the user taps on the cell named "Accountants"). Likewise, several categories need to be displayed and may or may not contain a further expansion of second level.
As for the web service, I've the option of fetching data in 2 ways:
- Getting both the main and sub categories in a single API (as shown in above screenshot).
- Getting the main and sub categories in separate APIs.
So far I've tried implementing the main categories in titleForHeaderInSection
& first-level sub-categories in cellForRowAtIndexPath
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[subCatArray objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ceLL" forIndexPath:indexPath];
cell.textLabel.text = [[subCatArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [catArray count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
for (int i=0; i<[catArray count]; i++) {
if(section == i){
return NSLocalizedString([catArray objectAtIndex:i], nil);
}
}
return nil;
}
But how do I go about the second-level and making these collapsible/expandable?