扩展和折叠tableView部分iOS? [英] Expanding and collapsing tableView sections iOS?

查看:134
本文介绍了扩展和折叠tableView部分iOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功扩展和折叠tableView部分,但到目前为止我无法对各个部分进行此操作。所有部分同时折叠或展开,这是因为我调用 [tableView reloadData] .So如何展开或折叠特定部分?

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;

}


推荐答案

isShowingList as

@property (nonatomic, strong) NSMutableArray *isShowingList;

为识别以前开设的部分,您需要另一处房产

And for identifying previously opened section you need another property

@property (nonatomic, assign) NSInteger openSectionIndex;

当你初始化数据时,在你的情况下isShowingList,初始化 isShowingList 重新加载表之前的数组

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]];
    }
} 

并初始化 openSectionIndex in viewDidLoad() like this

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;
}

这篇关于扩展和折叠tableView部分iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆