iOS tableview cell的展开收缩

本文展示了如何在iOS应用中实现UITableView的Cell展开和收缩功能。通过创建一个数据源数组和字典来存储状态,利用UITableViewDataSource和UITableViewDelegate方法定制视图。在点击header时,更新字典状态并使用reloadSections方法实现平滑动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{

    NSMutableArray *_allArray;//创建一个数据源数组

    NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开

}


@property (nonatomic,strong)UITableView *tableView;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    dic = [NSMutableDictionary dictionary];

    _allArray = [@[@[@"12",@"32",@"12",@"12",@"12",@"32"],@[@"12",@"12",@"12",@"32",@"12",@"12",@"12",@"32",@"12",@"12",@"12",@"32"],@[@"12",@"12",@"32"]]mutableCopy];

    [self.view addSubview:self.tableView];

}


//懒加载

- (UITableView *)tableView{

    if (!_tableView) {

        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 375, 667 - 64) style:UITableViewStylePlain];

        _tableView.delegate = self;

        _tableView.dataSource = self;

    }

    return _tableView;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _allArray.count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;

}

//

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [UIView new];

    view.backgroundColor = [UIColor redColor];

    //创建一个手势进行点击,这里可以换成button

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_tap:)];

    view.tag = 300 + section;

    [view addGestureRecognizer:tap];

    return view;

}


- (void)action_tap:(UIGestureRecognizer *)tap{

    NSString *str = [NSString stringWithFormat:@"%ld",tap.view.tag - 300];

    if ([dic[str] integerValue] == 0) {//如果是0,就把1赋给字典,打开cell

        [dic setObject:@"1" forKey:str];

    }else{//反之关闭cell

        [dic setObject:@"0" forKey:str];

    }

    // [self.tableView reloadData];

    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[str integerValue]] withRowAnimation:UITableViewRowAnimationFade];//有动画的刷新

    

    

}


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

    NSString *string = [NSString stringWithFormat:@"%ld",section];

    if ([dic[string] integerValue] == 1 ) {  //打开cell返回数组的count

        NSArray *array = [NSArray arrayWithArray:_allArray[section]];

        return array.count;

    }else{

        return 0;

    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 35;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }

    cell.backgroundColor = [UIColor orangeColor];

    cell.textLabel.text = _allArray[indexPath.section][indexPath.row];

    return cell;

}


@end



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值