Ios 如何在UITableView中展开和折叠特定级别的行?

Ios 如何在UITableView中展开和折叠特定级别的行?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,级别1的行应展开和折叠。假设第0行处于打开状态,即其状态为1,下一行的状态为0,如果我单击状态为0的下一行,则状态为1的行应关闭 我该怎么做 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; dictBtnStatus = [[NSMutableDictionary alloc] init]; arraylist = [[NSMutableArray alloc] init]; array5 = [[NSMuta

级别1的
行应展开和折叠。假设
第0行
处于打开状态,即其状态为
1
,下一行的状态为
0
,如果我单击状态为
0
的下一行,则状态为
1
的行应关闭

我该怎么做

这是我的密码:

- (void)viewDidLoad {
    [super viewDidLoad];
    dictBtnStatus = [[NSMutableDictionary alloc] init];
    arraylist = [[NSMutableArray alloc] init];
    array5 = [[NSMutableArray alloc] init];
    array1 = [[NSMutableArray alloc] init];
    objects = @[@"Medical Services In Chicago", @"Medical Services On Campus", @"ABC", @"California"];
    // objects = @[@"Medical Services In Chicago", @"Medical Services On Campus"];
    arrObjectValueChicago = @[@"Ronald McDonald® Children's Hospital of Loyola", @"Burn Centers", @"Gottlieb Hospitals"];
    arrObjectValueCampus = @[@"Cardinal Bernardin Cancer Center1", @"Center for Heart & Vascular Medicine2", @"ABC"];
    for (int i = 0; i < [arrObjectValueCampus count]; i++) {
        dictListCampus1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"2", @"level",[arrObjectValueCampus objectAtIndex:i], @"name", nil];
        [array5 addObject:dictListCampus1];
    }
    NSDictionary *dictListCampus = [[NSDictionary alloc] initWithObjectsAndKeys:@"Wellness Centers", @"name", @"1", @"level", array5, @"Objects", nil];
    NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:dictListCampus, nil];
    array3 = [[NSMutableArray alloc] init ];
    for (int i = 0; i < [arrObjectValueChicago count]; i++){
        dictList3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2", @"level",[arrObjectValueChicago objectAtIndex:i], @"name", nil];
        [array3 addObject:dictList3];
    }

    NSDictionary *dictList2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Hospitals", @"name", @"1", @"level", array3, @"Objects", nil];
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:dictList2, nil];
    for (int i = 0; i < [objects count]; i++) {
        if (i == 0) {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:0], @"name", @"0", @"level", array2, @"Objects", nil];
            [array1 addObject:dictionary];
        } else if (i == 1) {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:1], @"name", @"0", @"level", array6, @"Objects", nil];
            [array1 addObject:dictionary];
        } else if (i == 2) {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:2], @"name", @"0", @"level", array6, @"Objects", nil];
            [array1 addObject:dictionary];
        } else {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:3], @"name", @"0", @"level", array2, @"Objects", nil];
            [array1 addObject:dictionary];
        }
    }
    dictList = [[NSDictionary alloc] initWithObjectsAndKeys:array1, @"Objects", nil];
    arrayOriginal = [dictList valueForKey:@"Objects"];
    [arraylist addObjectsFromArray:arrayOriginal];
}

#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arraylist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // static NSString *CellIdentifier = @"CustomCellIdentifier";
    NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
   UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
   if (IndentLevel == 0) {
       CustomCellHeader *cell = (CustomCellHeader *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
       if (cell == nil) {
           [[NSBundle mainBundle]loadNibNamed:@"CustomCellHeader" owner:self options:nil];
           cell = self.headercell;
           self.headercell = nil;
        }
        cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:@"name"];
        [cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
        NSLog(@"indexFor level 0 ::%@ %d",cell.lblHeader.text,indexPath.row);
        return cell;
    } else if (IndentLevel == 1) {
        CustomCellSubHeader *cell = (CustomCellSubHeader *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        if (!cell) {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCellSubHeader" owner:self options:nil];
            cell = self.subheadercell;
            self.subheadercell = nil;
            NSLog(@"dicbtn %@", dictBtnStatus);
            NSString *strName = [[arraylist objectAtIndex:indexPath.row]valueForKey:@"name"];
            NSString *str = [dictBtnStatus objectForKey:strName];
            NSLog(@"indexFor level 1 ::%@ %d", strName, indexPath.row);

            if ([str isEqualToString:@"1"]) {
                [cell.btnarrow setImage:[UIImage imageNamed:@"dwn1_arow.png"] forState:UIControlStateNormal];
                [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_active.png"]];
            }
            cell.lblSubHeader.text = strName;
            cell.imgShadow.hidden = YES;
            [cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
        }
        return cell;
    } else if (IndentLevel == 2) {
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        if (cell == nil) {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
            cell = self.cells;
            self.cells = nil;
        }
        cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:@"name"];
        [cell.btnCall addTarget:self action:@selector(btnCall:) forControlEvents:UIControlEventTouchUpInside];
        [cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tblist deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
    NSString *strName = [[arraylist objectAtIndex:indexPath.row] valueForKey:@"name"];
    CustomCellSubHeader *cell = ((CustomCellSubHeader*)[tblist cellForRowAtIndexPath:indexPath]);
    if (IndentLevel == 1) {  
        [dictBtnStatus setValue:@"0" forKey:strName];
        [cell.btnarrow setImage:[UIImage imageNamed:@"gry_arrow.png"] forState:UIControlStateNormal];
        [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_default.png"]];
    }
    NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
    if ([d valueForKey:@"Objects"]) {
        NSArray *ar = [d valueForKey:@"Objects"];
        BOOL isAlreadyInserted = NO;
        for (NSDictionary *dInner in ar) {
            NSInteger index = [arraylist indexOfObjectIdenticalTo:dInner];
            isAlreadyInserted = (index > 0 && index != NSIntegerMax);
            if (IndentLevel == 1) {
                [dictBtnStatus setValue:@"0" forKey:strName];
                [cell.btnarrow setImage:[UIImage imageNamed:@"gry_arrow.png"] forState:UIControlStateNormal];
                [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_default.png"]];
            } else if (IndentLevel == 0) {
                NSString *strStatus = [dictBtnStatus objectForKey:strName];
                if ([strStatus isEqualToString:@"1"]) {
                // NSDictionary *dict = [arraylist objectAtIndex:previousRow];
                // if ([dict valueForKey:@"Objects"]) {
                    NSArray * array = [[arraylist objectAtIndex:previousRow]valueForKey:@"Objects"];
                    [self miniMizeThisRows:array];
                }
            }
            if (isAlreadyInserted) break;
        }
        if (isAlreadyInserted) {
            if ([arraylist count] - 1 && IndentLevel == 1) {
                cell.imgShadow.hidden = NO;
            }
            if (IndentLevel == 1) {
                [dictBtnStatus setValue:@"0" forKey:strName];
                [cell.btnarrow setImage:[UIImage imageNamed:@"gry_arrow.png"] forState:UIControlStateNormal];
                [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_default.png"]];
            } else if (IndentLevel == 0) {
                [dictBtnStatus setValue:@"0" forKey:strName];
            }
            [self miniMizeThisRows:ar];
        } else {
            NSUInteger count=indexPath.row + 1;
            NSMutableArray *arCells = [NSMutableArray array];
            for (NSDictionary *dInner in ar) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                previousRow  = [indexPath row];
                if (IndentLevel == 1) {
                    [dictBtnStatus setValue:@"1" forKey:strName];
                    [cell.btnarrow setImage:[UIImage imageNamed:@"dwn1_arow.png"] forState:UIControlStateNormal];
                    [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_active.png"]];
                } else if (IndentLevel == 0) {
                    [dictBtnStatus setValue:@"1" forKey:strName];
                }
                [arraylist insertObject:dInner atIndex:count++];
            }
        [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
    if (indentLevel == 0) {
        return 40;
    } else if (indentLevel == 1) {
        return 25;
    } else if (indentLevel == 2) {
        CustomCell *cell = ((CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]);
        CGSize maximumSize = CGSizeMake(300, 9999);
        UIFont *myFont = [UIFont fontWithName:@"Arial" size:11.5];
        CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
        [cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];

        if (myStringSize.height > 80) {
            myStringSize.height = 50;
            [cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
            cell.txtAddress.scrollEnabled = YES;
        } else {
            cell.txtAddress.scrollEnabled = YES;
            myStringSize.height = 40;
        }
        return myStringSize.height+ 50;
    } else return 25;
}

#pragma mark - TableAnimation

- (void)miniMizeThisRows:(NSArray *)ar {
    for(NSDictionary *dInner in ar) {
        NSUInteger indexToRemove = [arraylist indexOfObjectIdenticalTo:dInner];
        NSArray *arInner = [dInner valueForKey:@"Objects"];
        if (arInner && [arInner count] > 0) {
            [self miniMizeThisRows:arInner];
        }      
        if ([arraylist indexOfObjectIdenticalTo:dInner]!= NSNotFound) {
            [arraylist removeObjectIdenticalTo:dInner];
            [tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}
-(void)viewDidLoad{
[超级视图下载];
dictBtnStatus=[[NSMutableDictionary alloc]init];
arraylist=[[NSMutableArray alloc]init];
array5=[[NSMutableArray alloc]init];
array1=[[NSMutableArray alloc]init];
对象=@[@“芝加哥医疗服务”、@“校园医疗服务”、@“ABC”、@“加利福尼亚”];
//对象=@[@“芝加哥医疗服务”,“校园医疗服务”];
arrObjectValueChicago=@[@“罗纳德·麦当劳洛约拉儿童医院”@“烧伤中心”@“戈特利布医院”];
arrObjectValueCampus=@[@“红衣主教伯纳德癌症中心1”,“心脏与血管医学中心2”,“ABC”];
对于(int i=0;i<[arobjectvaluecampus count];i++){
dictListCampus1=[[NSDictionary alloc]initWithObjectsAndKeys:@“2”,“级别”,“arrObjectValueCampus对象索引:i],@“name”,nil];
[array5 addObject:dictListCampus1];
}
NSDictionary*dictListCampus=[[NSDictionary alloc]initWithObjectsAndKeys:@“健康中心”@“名称”@“1”@“级别”,数组5,@“对象”,零];
NSMutableArray*array6=[[NSMutableArray alloc]initWithObjects:dictListCampus,nil];
array3=[[NSMutableArray alloc]init];
for(int i=0;i<[arObjectValueChicago count];i++){
dictList3=[[NSDictionary alloc]initWithObjectsAndKeys:@“2”,“level”,“arrObjectValueChicago objectAtIndex:i],@“name”,nil];
[array3 addObject:dictList3];
}
NSDictionary*dictList2=[[NSDictionary alloc]initWithObjectsAndKeys:@“Hospitals”、@“name”、@“1”、@“level”、array3、@“Objects”、nil];
NSMutableArray*array2=[[NSMutableArray alloc]initWithObjects:dictList2,nil];
对于(int i=0;i<[对象计数];i++){
如果(i==0){
dictionary=[[NSDictionary alloc]initWithObjectsSandKeys:[objects objectAtIndex:0]、@“name”、“0”、“level”、“array2”、“objects”、“nil];
[array1 addObject:dictionary];
}else如果(i==1){
dictionary=[[NSDictionary alloc]initWithObjectsSandKeys:[objects对象索引:1]、@“名称”、“0”、“级别”、“数组6”、“对象”、“零];
[array1 addObject:dictionary];
}else如果(i==2){
dictionary=[[NSDictionary alloc]initWithObjectsSandKeys:[objects对象索引:2]、@“名称”、“0”、“级别”、“数组6”、“对象”、nil];
[array1 addObject:dictionary];
}否则{
dictionary=[[NSDictionary alloc]initWithObjectsSandKeys:[ObjectsObjectAtIndex:3],@“名称”,“0”,“级别”,“数组2”,“对象”,零];
[array1 addObject:dictionary];
}
}
dictList=[[NSDictionary alloc]initWithObjectsAndKeys:array1,@“Objects”,nil];
arrayOriginal=[dictList valueForKey:@“Objects”];
[arraylist addObjectsFromArray:arrayOriginal];
}
#pragma标记-TableView
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[arraylist count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//静态NSString*CellIdentifier=@“CustomCellIdentifier”;
nsuiger IndentLevel=[[arraylist objectAtIndex:indexath.row]valueForKey:@“level”]intValue];
UITableViewCell*单元格=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@“cell]”;
if(IndentLevel==0){
CustomCellHeader*单元格=(CustomCellHeader*)[tableView dequeueReusableCellWithIdentifier:@“CellIdentifier”];
如果(单元格==nil){
[[NSBundle mainBundle]loadNibNamed:@“CustomCellHeader”所有者:自选项:nil];
cell=self.headercell;
self.headercell=nil;
}
cell.lblHeader.text=[[arraylist objectAtIndex:indexPath.row]valueForKey:@“name”];
[cell SetIndicationLevel:[[[arraylist objectAtIndex:indexPath.row]valueForKey:@“level”]intValue];
NSLog(@“indexforlevel0::%@%d”,cell.lblHeader.text,indexPath.row);
返回单元;
}否则如果(缩进级别==1){
CustomCellSubHeader*单元格=(CustomCellSubHeader*)[tableView dequeueReusableCellWithIdentifier:@“CellIdentifier”];
如果(!单元格){
[[NSBundle mainBundle]loadNibNamed:@“CustomCellSubHeader”所有者:自选项:nil];
单元格=自身子标题单元格;
self.subheadercell=零;
NSLog(@“dicbtn%@”,dictBtnStatus);
NSString*strName=[[arraylist objectAtIndex:indexPath.row]valueForKey:@“name”];
NSString*str=[dictBtnStatus objectForKey:strName];
NSLog(@“indexforlevel1::%@%d”,strName,indexPath.row);
if([str isEqualToString:@“1”]){
[cell.btRown setImage:[UIImage ImageName:@“dwn1_arow.png”]用于状态:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage ImageName:@“tab_active.png”];
}
cell.lblSubHeader.text=strName;
cell.imgShadow.hidden=是;
[cell SetIndicationLevel:[[[arraylist objectAtIndex:indexPath.row]valueForKey:@“level”]intValue];
}
返回单元;
}否则如果(缩进级别==2){
CustomCell*单元格=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:@“CellIdentifier”];
如果(单元格==nil){
[[NSBundle mainBundle]loadNibNamed:@“CustomCell”所有者:自选项:nil];
细胞=自身细胞;
self.cells=nil;
}
cell.txtAddress.text=[[arraylist objectAtIndex:indexath.row]valueForKey:
- (void)viewDidLoad {
    [super viewDidLoad];
    dictBtnStatus = [[NSMutableDictionary alloc] init];
    arraylist = [[NSMutableArray alloc] init];
    array5 = [[NSMutableArray alloc] init];
    array1 = [[NSMutableArray alloc] init];
    objects = @[@"Medical Services In Chicago", @"Medical Services On Campus", @"ABC", @"California"];
    // objects = @[@"Medical Services In Chicago", @"Medical Services On Campus"];
    arrObjectValueChicago = @[@"Ronald McDonald® Children's Hospital of Loyola", @"Burn Centers", @"Gottlieb Hospitals"];
    arrObjectValueCampus = @[@"Cardinal Bernardin Cancer Center1", @"Center for Heart & Vascular Medicine2", @"ABC"];
    for (int i = 0; i < [arrObjectValueCampus count]; i++) {
        dictListCampus1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2", @"level",[arrObjectValueCampus objectAtIndex:i], @"name", nil];
        [array5 addObject:dictListCampus1];
    }
    NSDictionary *dictListCampus = [[NSDictionary alloc]initWithObjectsAndKeys:@"Wellness Centers", @"name", @"1", @"level", array5, @"Objects", nil];
    NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:dictListCampus, nil];
    NSDictionary *dictListCampus2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Wellness Centers123", @"name", @"1", @"level", array5, @"Objects", nil];
    NSMutableArray *array61 = [[NSMutableArray alloc] initWithObjects:dictListCampus2, nil];
    array3 = [[NSMutableArray alloc] init];
    for (int i = 0; i < [arrObjectValueChicago count]; i++) {
        dictList3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"2", @"level",[arrObjectValueChicago objectAtIndex:i], @"name", nil];
        [array3 addObject:dictList3];
    }

    NSDictionary *dictList2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Hospitals", @"name", @"1", @"level", array3, @"Objects", nil];
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:dictList2, nil];
    for (int i = 0; i < [objects count]; i++) {
        if (i == 0) {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:0], @"name", @"0", @"level", array2, @"Objects", nil];
            [array1 addObject:dictionary];
        } else if (i == 1) {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:1], @"name", @"0", @"level", array6, @"Objects", nil];
            [array1 addObject:dictionary];
        } else if (i == 2) {
            dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:2], @"name", @"0", @"level", array61, @"Objects", nil];
            [array1 addObject:dictionary];
        }
      // else {
      //    dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:3], @"name", @"0", @"level", array2, @"Objects", nil];
      //    [array1 addObject:dictionary];
      //}
    }
    dictList = [[NSDictionary alloc] initWithObjectsAndKeys:array1, @"Objects", nil];
    NSLog(@"DictList :: %@", dictList);
    arrayOriginal = [dictList valueForKey:@"Objects"];
    NSLog(@"Array Original :: %@", arrayOriginal);
    [arraylist addObjectsFromArray:arrayOriginal];

    //[tblist.delegate tableView:tblist didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    //[tblist.delegate tableView:tblist didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
}

#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return [arraylist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //static NSString *CellIdentifier = @"CustomCellIdentifier";
    NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (IndentLevel == 0) {
        CustomCellHeader *cell = (CustomCellHeader *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        if (cell == nil) {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCellHeader" owner:self options:nil];
            cell = self.headercell;
            self.headercell = nil;
        }
        cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:@"name"];
        [cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
        NSLog(@"indexFor level 0 ::%@ %d",cell.lblHeader.text, indexPath.row);
        return cell;
    } else if (IndentLevel == 1) {
        CustomCellSubHeader *cell = (CustomCellSubHeader *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        if (!cell) {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCellSubHeader" owner:self options:nil];
            cell = self.subheadercell;
            self.subheadercell = nil;
            NSLog(@"dicbtn %@", dictBtnStatus);
        }
        NSString *strName = [[arraylist objectAtIndex:indexPath.row] valueForKey:@"name"];
        NSString *str = [dictBtnStatus objectForKey:strName];
        NSLog(@"indexFor level 1 ::%@ %d", strName, indexPath.row);
        if ([str isEqualToString:@"1"]) {
            [cell.btnarrow setImage:[UIImage imageNamed:@"dwn1_arow.png"] forState:UIControlStateNormal];
            [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_active.png"]];
        }
      // else {
      //    [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_default.png"]];
      //}
        cell.lblSubHeader.text = strName;
        [cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];

        if (indexPath.row == arraylist.count - 1) {
            cell.imgShadow.hidden = NO;
        }
        return cell;
    } else if (IndentLevel == 2) {
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        if (cell == nil) {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
            cell = self.cells;
            self.cells = nil;
        }
        cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:@"name"];
        [cell.btnCall addTarget:self action:@selector(btnCall:) forControlEvents:UIControlEventTouchUpInside];
        [cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCellSubHeader *cell;
    if ([[tblist cellForRowAtIndexPath:indexPath] isKindOfClass:[CustomCellSubHeader class]]) {
        cell = (CustomCellSubHeader*)[tblist cellForRowAtIndexPath:indexPath];
        UIImage *img = [cell.btnarrow imageForState:UIControlStateNormal];
        if ([img isEqual:[UIImage imageNamed:@"dwn1_arow.png"]]) {
            [cell.btnarrow setImage:[UIImage imageNamed:@"gry_arrow.png"] forState:UIControlStateNormal];
            [dictBtnStatus setValue:@"0" forKey:cell.lblSubHeader.text];
            [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_default.png"]];
            cell.imgShadow.hidden = NO;
        } else {
            [cell.btnarrow setImage:[UIImage imageNamed:@"dwn1_arow.png"] forState:UIControlStateNormal];
            dictBtnStatus = [[NSMutableDictionary alloc] init];
            [dictBtnStatus setValue:@"1" forKey:cell.lblSubHeader.text];
            [cell.imgSubHeader setImage:[UIImage imageNamed:@"tab_active.png"]];
            cell.imgShadow.hidden = YES;
        }
    }
    NSMutableArray *arrIndex = [[NSMutableArray alloc] init];
    NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
    if (IndentLevel == 0) {
        arrLast = [[NSMutableArray alloc] init];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
    if ([d valueForKey:@"Objects"]) {
        NSArray *ar = [d valueForKey:@"Objects"];
        BOOL isAlreadyInserted = NO;
        for (NSDictionary *dInner in ar) {
            NSInteger index = [arraylist indexOfObjectIdenticalTo:dInner];
            isAlreadyInserted = (index > 0 && index != NSIntegerMax);
            if (isAlreadyInserted) break;
        }
        if (isAlreadyInserted) {
            [self miniMizeThisRows:ar];
        } else {
            NSUInteger count = indexPath.row + 1;
            NSMutableArray *arCells = [NSMutableArray array];
            for (NSDictionary *dInner in ar) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [arrLast addObject:dInner];
                [arraylist insertObject:dInner atIndex:count++];
            }
            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationBottom];
        }
    }
    //NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
    if (IndentLevel == 0) {
        NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
        for (int i = 0 ; i < arraylist.count; i++) {
            NSDictionary *dic = [arraylist objectAtIndex:i];
            if ([[dic valueForKey:@"level"] intValue] != 0) {
                NSInteger index = [arrLast indexOfObjectIdenticalTo:dic];
                if ((index >= 0 && index != NSIntegerMax)) {
                } else {
                    [discardedItems addIndex:i];
                    [arrIndex addObject:[NSIndexPath indexPathForRow:i inSection:0]];
                }
            }
        }
        if (discardedItems.count > 0) {
            [arraylist removeObjectsAtIndexes:discardedItems];
            [tblist deleteRowsAtIndexPaths:arrIndex withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:@"level"] intValue];
    if (indentLevel == 0) {            
        return 40;
    } else if (indentLevel == 1) {
        return 25;
    } else if (indentLevel == 2) {
        CustomCell *cell = ((CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]);
        CGSize maximumSize = CGSizeMake(300, 9999);
        UIFont *myFont = [UIFont fontWithName:@"Arial" size:11.5];
        CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
        [cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];

        if (myStringSize.height > 80) {
            myStringSize.height = 50;
            [cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
            cell.txtAddress.scrollEnabled = YES;
        } else {
            cell.txtAddress.scrollEnabled = YES;
            myStringSize.height = 40;
        }
        return myStringSize.height + 50;
    } else return 25;
}

#pragma mark - TableAnimation
- (void)miniMizeThisRows:(NSArray *)ar {
    for (NSDictionary *dInner in ar) {
        NSUInteger indexToRemove = [arraylist indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"Objects"];
        if (arInner && [arInner count] > 0) {
            [self miniMizeThisRows:arInner];
        }

        if ([arraylist indexOfObjectIdenticalTo:dInner] != NSNotFound) {
            [arraylist removeObjectIdenticalTo:dInner];
            [tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}

#pragma mark - Buttons
- (IBAction)btnCall:(id)sender {
    UIAlertView *Notpermitted = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you want to call on this number." delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
    Notpermitted.delegate = self;
    [Notpermitted tag];
    [Notpermitted show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *strCall = [NSString stringWithFormat:@"tel://999-999-9999"];
        NSURL *url = [NSURL URLWithString:strCall];
        UIDevice *device = [UIDevice currentDevice];
        if ([[device model] isEqualToString:@"iPhone"] ) {
            [[UIApplication sharedApplication] openURL:url];
        } else {
            UIAlertView *Notpermitted = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [Notpermitted show];
        }
    }
}