美文网首页
cell的展开和收起

cell的展开和收起

作者: 为什么划船不靠桨 | 来源:发表于2018-01-31 17:00 被阅读0次

在项目里我们经常会用到cell的展开和收起,就像下面这样,今天记录一下


cell收缩和展开.gif

实现代码

#import "MineViewController.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _flagArr[5];
    
    NSArray  *_titleArr;
    NSArray  *_contentArr;
}
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _titleArr = @[@"111",@"222",@"333",@"444",@"555"];
    _contentArr = @[@"第一项内容111111",@"第二项内容222222",@"第三项内容333333",@"第四项内容444444",@"第五项内容555555"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_flagArr[section] == YES){
        return 1;
    }else {
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _contentArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat rowHeight = [_contentArr[indexPath.row] boundingRectWithSize:CGSizeMake(SCREENWIDTH, 1000) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
    return rowHeight + 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc]initWithFrame:self.view.frame];
    view.backgroundColor = [UIColor whiteColor];
    
    //创建一个透明的按钮将试图覆盖
    UIButton * questionBtn = [[UIButton alloc]initWithFrame:view.frame];
    questionBtn.tag= section;
    [questionBtn addTarget:self action:@selector(clickToOpen:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:questionBtn];
    
    //添加一条下划线
    UIImageView * lineImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 39.5, SCREENWIDTH, 0.5)];
    lineImg.backgroundColor = [UIColor grayColor];
    [questionBtn addSubview:lineImg];
    
    //添加箭头
    UIImageView * redSanjiaoImg = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH - 30, 15, 10, 10)];
    redSanjiaoImg.image = [UIImage imageNamed:@"btn_forward_nightmode_8x12_"];
    [questionBtn addSubview:redSanjiaoImg];
    
    //标题
    UILabel * labView = [[UILabel alloc]initWithFrame:CGRectMake(40, 10, SCREENWIDTH - 60, 20)];
    labView.text = [_titleArr objectAtIndex:section];
    [questionBtn addSubview:labView];
    
    if (_flagArr[section] == YES){
        //旋转90度
        redSanjiaoImg.transform = CGAffineTransformMakeRotation(M_PI_2);
    }else{
        //恢复成原来的状态
        redSanjiaoImg.transform = CGAffineTransformIdentity;
    }
    return view;
}

- (void)clickToOpen:(UIButton *)btn{
    
    _flagArr[btn.tag] = !_flagArr[btn.tag];
    
    //通过按钮的tag进行刷新
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [_contentArr objectAtIndex:indexPath.section];
    cell.clipsToBounds = YES;
    cell.layer.borderWidth = 0.5;
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.textLabel.numberOfLines = 0;
    return cell;
}
@end

相关文章

网友评论

      本文标题:cell的展开和收起

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