I am following a tutorial,where I can expand the table view by adding some sub-cells and collapse the table by removing the sub-cells. I am trying to change how the expand operation should execute. When I tap on a row,it expand and shows the sub-cells,and when I tap on other row,the previous expanded row should close. I am not able to do this . I tried the following but I couldnt make the one row expandable at a time,and the other row should close when one expands.

Note: This code works fine,but to collapse the row,we need to tap twice on the same row. I am trying to collapse when other parent is tapped.

Here is the code:

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

    Node *node = [self nodeForIndexPath:indexPath];

// differenet table view cell prototypes for different node levels

UITableViewCell *cell;
if(node.level  == 0)

{
    cell = [tableView dequeueReusableCellWithIdentifier:@"level1cell" forIndexPath:indexPath];
}
else
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"level2cell" forIndexPath:indexPath];
}

// set the nodes title as row text
cell.textLabel.text = node.title;

// attach the nodeId for later lookup when selected
cell.tag = node.nodeId;

// Configure the cell...

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
Node *node = [self nodeForIndexPath:indexPath];
  //  NSLog(@"node id is %ld level is %ld and indexpath.row is %d",(long)node.nodeId,(long)node.level,indexPath.row);

    node.expanded = !node.expanded;

if (node.level==0) {
    NSLog(@"you tapped parent");
    //now check other parents are expanded or not
}else{

    NSLog(@"you tapped child");
}

//insertion always happen after deletion
//   NSLog(@"you touched at %@,index row is %d",indexPath,indexPath.row);
if(node.expanded )
{

    // add n rows
    NSMutableArray *indexPaths = [NSMutableArray array];
    for(NSInteger i=indexPath.row; i< indexPath.row+node.subNodes.count; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
    }

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}
else
{

    //you store the node refe after row is expanded

    // delete n rows
    NSMutableArray *indexPaths = [NSMutableArray array];
    for(NSInteger i=indexPath.row; i< indexPath.row+node.subNodes.count; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i+1 inSection:0]];
    }

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}

}

 #pragma mark - Private helper

  -(Node*) nodeForIndexPath:(NSIndexPath*)indexPath
  {
int idx = 0;

for(Node *node in _nodes)
{
    if(idx == indexPath.row)
    {
        return node;
    }
    idx++;

    if(node.expanded)
    {
        for (Node *subNode in node.subNodes)
        {
            if(idx == indexPath.row)
            {
                return subNode;
            }
            idx++;
        }
    }
}
return nil;
}

```