Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to expand and collapse rows at particular level in UITableView?
    text
    copied!<p>Rows of <code>Level 1</code> should expand and collapse. Suppose if <code>Row 0</code> is open - that means its status is <code>1</code> - and next row's status is <code>0</code>, if I click on next row whose status is <code>0</code>, then the row whose status is <code>1</code> should get closed. </p> <p>How can I do that? </p> <p>Here is my code:</p> <pre><code>- (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 &amp; Vascular Medicine2", @"ABC"]; for (int i = 0; i &lt; [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 &lt; [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 &lt; [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 &gt; 0 &amp;&amp; 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 &amp;&amp; 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 &gt; 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 &amp;&amp; [arInner count] &gt; 0) { [self miniMizeThisRows:arInner]; } if ([arraylist indexOfObjectIdenticalTo:dInner]!= NSNotFound) { [arraylist removeObjectIdenticalTo:dInner]; [tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload