how to create Expandable Table view like Tree Structure in ios

13,397

Solution 1

try this :-

  NSMutableIndexSet *expandedSections;
  @property (strong, nonatomic) NSIndexPath *expandedIndexPath;



 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
   {

    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
        return 100;
    }
    else
    {
    return 30;
    }

}


  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {

  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
      [tableView beginUpdates];
      self.expandedIndexPath = nil;
      [tableView endUpdates];
  }
  else{
      self.expandedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

      [tableView beginUpdates];

     if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame)     {
        self.expandedIndexPath = indexPath;
    } else {
        self.expandedIndexPath = nil;
    }

    [tableView endUpdates];
   }

 }

Solution 2

there is UITreeView example at github UITreeView

Share:
13,397
Test
Author by

Test

Updated on July 20, 2022

Comments

  • Test
    Test almost 2 years

    hi every one i need a expandable table view for my data, Is it Possible to create like this in tableView.In my Data each one having different Childs,below is my data

    -A1
        -A1.1
            -A1.1.1
                A1.1.1.1
    +B1
    +C1
    +D1
    
    ----------------------
    +A1
    -B1
        +B1.1
    +C1
    +D1
    -----------------------
    +A1
    +B1
    +C1
    -D1
        +D1.1
        -D1.2
            +D1.2.1
            +D1.2.2
        +D1.3
    

    Help me thanks in advance