美文网首页
iOS表的展开和关闭,单开表

iOS表的展开和关闭,单开表

作者: 路有点颠簸 | 来源:发表于2019-12-04 16:51 被阅读0次
    IMG_0103.PNG

    思路:定义一个lastIndexPath存上次点击的区头header,定义一个数组sectionTypeArr,存每个区头的开或关,我这里存字符串0和1

    self.sectionTypeArr = [NSMutableArray array];
        for (int i = 0; i<10; i++) {
            // 0-关闭 1-打开
            [self.sectionTypeArr addObject:@"0"];
        }
    

    section的点击事件,处理区头的展开和关闭

    - (void)sectionClick:(UITapGestureRecognizer *)gesture{
        LHWalletTransacationRecordHeaderView *headerView = (LHWalletTransacationRecordHeaderView *)gesture.view;
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathWithIndex:headerView.tag];
        
        NSString *type = self.sectionTypeArr[headerView.tag];
        if ([type isEqualToString:@"0"]) {
            //之前关闭
            type = @"1";
            
            if (self.lastIndexPath) {
                [self.sectionTypeArr replaceObjectAtIndex:self.lastIndexPath.section withObject:@"0"];
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:self.lastIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            }
        }else{
            //之前打开的
            type = @"0";
        }
        [self.sectionTypeArr replaceObjectAtIndex:headerView.tag withObject:type];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:headerView.tag] withRowAnimation:UITableViewRowAnimationFade];
        self.lastIndexPath = currentIndexPath;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.sectionTypeArr.count;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        NSString *type = self.sectionTypeArr[section];
        return [type isEqual:@"0"]?0:4;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 45;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        LHWalletTransacationRecordHeaderView *headerView = [LHWalletTransacationRecordHeaderView new];
        
        NSString *type = self.sectionTypeArr[section];
        headerView.orOPen = [type isEqualToString:@"1"]?YES:NO;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionClick:)];
        headerView.tag = section;
        [headerView addGestureRecognizer:tap];
        return headerView;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        return cell;
    }
    

    相关文章

      网友评论

          本文标题:iOS表的展开和关闭,单开表

          本文链接:https://www.haomeiwen.com/subject/xoylgctx.html