0

please suggest me how to display data like tree view.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return marrSearchResult.count;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   int iCount=0;

    if (section==0) {

        iCount =(int)[marrSearchResult count];
    }else if(section==1){

        iCount =(int)[[[[marrSearchResult objectAtIndex:section] valueForKey:@"data"] valueForKey:@"result"] count]+1;
    }else{

    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *simpleTableIdentifier = @"MyTravelProfileTblCELL";

    if (indexPath.section == 0) {
    }else{
    }
}

see below image desplay like this enter image description here

Dharma
  • 3,007
  • 3
  • 23
  • 38
Chirag Sondagar
  • 427
  • 1
  • 4
  • 15

2 Answers2

2

Follow these steps :

  • Create custom tableview cell for section header (Tourist attractions, Government/Community). Lets call it PlaceTypeCell
  • Create another custom tableview cell for rows (Museums, Hospitals... ). Lets call it PlaceNameCell.
  • Return the section count as the count of main array (you already did it)
  • Return the row count as the count of subarray at the index of section in the main array (also you did it)
  • Now in the cellForRowAtIndexPath, get the sub array from main based on section number (indexPath.section). Now get the required row item(Place name and details) from sub array using row number (indexPath.row). Using the data in that row item populate the table cell.
  • Now in the viewForHeaderInSection get the place type details from the main array using section number and populate the PlaceTypeCell and return as section header. Have a look at Best way to create custom UITableview section header in storyboard
  • Set the height for section header using heightForHeaderInSection

Hope it helps.

Edit : If you can show the format of marrSearchResult, then it will be easy to help.

Ravi
  • 2,441
  • 1
  • 11
  • 30
1

Create Input array in Format:

NSArray *tourismSection = @[@"Museums", @"Aquariums"];
NSArray *govSection = @[@"Hopsital"];

NSDictionary *data = @{
     @"tourism" : tourismSection,
     @"gov"     : govSection
};
NSArray *inputs = @[@"tourism",@"gov" ];

in numberOfSectionsInTableView

return inputs.count;

in numberOfRowsInSection:

return [data valueForKey:inputs[indexPath.section]].count;
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84