2

I want to expand/collapse table view sections.I googled and found a code and its working fine.but the problem is the previously opened section is not closed when a new section is opened.Thanks

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
    BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
    collapsed       =  !collapsed;
    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
    //reload specific section animated
    NSRange range   = NSMakeRange(indexPath.section, 1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
    [self.aTableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
}

I tried the following code which is working but the animation is not cool as all the sections are reloading.

NSMutableArray *isSectionTouched =[[NSMutableArray alloc]initWithCapacity:arrayForBool.count];
isSectionTouched=[arrayForBool mutableCopy];

for(int i = 1; i <[arrayForBool count] ; i ++){
    if(i != gestureRecognizer.view.tag){
        [isSectionTouched replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
    }else{
        if ([[isSectionTouched objectAtIndex:gestureRecognizer.view.tag]boolValue]==YES) {
            [isSectionTouched replaceObjectAtIndex:gestureRecognizer.view.tag withObject:[NSNumber numberWithBool:NO]];
        }else if ([[isSectionTouched objectAtIndex:gestureRecognizer.view.tag]boolValue]==NO){
            [isSectionTouched replaceObjectAtIndex:gestureRecognizer.view.tag withObject:[NSNumber numberWithBool:YES]];
        }
    }
}
arrayForBool=isSectionTouched;
NSRange range   = NSMakeRange(1,arrayForBool.count - 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
Chandra Sekhar
  • 177
  • 1
  • 11
  • This question could help you http://stackoverflow.com/questions/1938921/expand-collapse-section-in-uitableview – rdelfin Jul 16 '13 at 13:51

1 Answers1

1

Just before you replace the selected index path, iterate over arrayForBool and set each item to NO. You could hold a property to store the currently open section index but unless you have hundreds of sections it isn't worth it.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I did that but while reloading the table view the animation doesnt look good.I have updated the question please check – Chandra Sekhar Jul 17 '13 at 09:01
  • 2
    In your loop, build an array of the section indexes that have changed and just reload them. You don't need to reload all of them... – Wain Jul 17 '13 at 09:11
  • even if it is forbidden i just give props to wain, sir whenever i find an answer of yours, it's dead precise you are helping a lot of rookies out there, thanks ! – Hakeem El Bakka-lee Dec 04 '13 at 14:43