Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - Expanding and collapsing tableView sections iOS?

I am able to expand and collapse the tableView sections successfully however I am not able to do it for individual sections so far.So all the sections collapse or expand at the same time, which is because I call [tableView reloadData] .So How can I expand or collapse a particular section?

Here is how I am doing it currently.

  -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  {
     headerLabel = [[UILabel alloc]init];
     headerLabel.tag = section;
     headerLabel.userInteractionEnabled = YES;
     headerLabel.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2];
     headerLabel.text = [menuCategoryArray objectAtIndex:section];
     headerLabel.frame = CGRectMake(5, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height);


     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerClicked:)];
     tapGesture.cancelsTouchesInView = NO;
     [headerLabel addGestureRecognizer:tapGesture];

     return headerLabel;


 }

 -(void)headerClicked:(UIGestureRecognizer*)sender
 {

    if (!isShowingList) {
    isShowingList=YES;
    [self.menuTableView reloadData];
    UILabel *lbl = (UILabel*)sender.view;
    NSLog(@"header no : %d", lbl.tag);

   }else{

    isShowingList=NO;
    [self.menuTableView reloadData];
    UILabel *lbl = (UILabel*)sender.view;
    NSLog(@"header no : %d", lbl.tag);

  }


  }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (isShowingList) {

    return [[[[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"] objectAtIndex:section] count];

}else{
    return 0;
}
return 0;

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First take isShowingList as

@property (nonatomic, strong) NSMutableArray *isShowingList;

And for identifying previously opened section you need another property

@property (nonatomic, assign) NSInteger openSectionIndex;

when you have the data initialized, isShowingList in you case, initialize isShowingList array before reloading table

self.isShowingList = [NSMutableArray array];
if (jsonArray && [jsonArray valueForKey:@"menus"] && [[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"]) {
    for (int i = 0; i < [[[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"] count]; i++) {
        [self.isShowingList addObject:[NSNumber numberWithBool:NO]];
    }
} 

and initialize openSectionIndex in viewDidLoad() like this

self.openSectionIndex = NSNotFound;

and your code should be changed like this

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([[self.isShowingList objectAtIndex:section] boolValue]) {
        return [[[[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"] objectAtIndex:section] count];
    } else {
        return 0;
    }
    return 0;
}

-(void)headerClicked:(UIGestureRecognizer*)sender {
    UILabel *lbl = (UILabel*)sender.view;
    NSLog(@"header no : %d", lbl.tag);
    if ([[self.isShowingList objectAtIndex:lbl.tag] boolValue]) {
        [self closeSection:lbl.tag];
    } else {
        [self openSection:lbl.tag];
    }
}

//methods for expanding and collapsing sections
- (void)openSection:(NSInteger)section {
    [self.isShowingList replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:YES]];
    NSInteger countOfRowsToInsert = [[[[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"] objectAtIndex:section] count];
    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
    }
    NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
    NSInteger previousOpenSectionIndex = self.openSectionIndex;
    if (previousOpenSectionIndex != NSNotFound) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.menuTableView reloadSections:[NSIndexSet indexSetWithIndex:previousOpenSectionIndex] withRowAnimation:UITableViewRowAnimationNone];
        });
        [self.isShowingList replaceObjectAtIndex:previousOpenSectionIndex withObject:[NSNumber numberWithBool:NO]];
        NSInteger countOfRowsToDelete = [[[[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"] objectAtIndex:previousOpenSectionIndex] count];
        for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]];
        }
    }
    // Apply the updates.
    [self.menuTableView beginUpdates];
    [self.menuTableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.menuTableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.menuTableView endUpdates];
    self.openSectionIndex = section;
}

- (void)closeSection:(NSInteger)section {
    [self.isShowingList replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:NO]];
    NSInteger countOfRowsToDelete = [self.menuTableView numberOfRowsInSection:section];
    if (countOfRowsToDelete > 0) {
        NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
        }
        [self.menuTableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
    }
    self.openSectionIndex = NSNotFound;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...