iOS tableview cell 的展开收缩

 

iOS tableview cell 的展开收缩

 

#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 = [[UITableViewalloc]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 = [[UITapGestureRecognizeralloc]initWithTarget:self action:@selector(action_tap:)];

    

    view.tag = 300 + section;

    

    [view addGestureRecognizer:tap];

    

    return view;

    

}

 

 

 

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

    

    NSString *str = [NSStringstringWithFormat:@"%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 = [NSStringstringWithFormat:@"%ld",section];

    

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

        

        NSArray *array = [NSArrayarrayWithArray:_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 = [tableViewdequeueReusableCellWithIdentifier:@"cell"];

    

    if (!cell) {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:@"cell"];

        

    }

    

    cell.backgroundColor = [UIColor orangeColor];

    

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

    

    return cell;

    

}

 

 

 

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中的tableView是一种用于展示大量数据的常见控件,而多级展开折叠则是一种实现在tableView中展示层级关系的常见需求,类似于QQ好友列表的效果。 实现这个效果的方法是通过tableView的数据源和代理方法来控制每个cell的样式和交互。以下是一个简单的实现步骤: 1. 设计数据模型:根据层级关系设计一个合适的数据模型,每个模型中包含展开折叠状态、子模型等相关属性。 2. 创建tableView:在界面上创建一个tableView,并设置其数据源和代理为当前控制器。 3. 实现数据源方法:在数据源方法中,根据模型的展开折叠状态来确定每个cell的样式和数量。 4. 实现代理方法:在代理方法中,根据用户的操作(点击或者滑动等)来更新模型的展开折叠状态,并刷新tableView。 5. 自定义cell:根据不同的展开折叠状态,自定义cell的样式和布局。 6. 添加动画效果:为了增加用户体验,在展开折叠的过程中可以添加一些动画效果,比如cell展开折叠tableView的滚动等。 7. 处理点击事件:根据用户的点击来判断是展开还是折叠,并更新对应的模型和tableView。 8. 优化性能:如果数据量较大,可以考虑使用懒加载或者异步加载等方式来减少内存占用和提高性能。 通过以上步骤,我们就可以实现类似QQ好友列表的多级展开折叠效果。当用户点击某个cell,可以展开其子级内容,再次点击则折叠子级内容,以此类推,完成多级展开折叠的功能。 需要注意的是,实现这个效果需要根据具体的需求和设计来确定数据模型、点击事件和界面样式等方面的实现细节。以上是一个基本的实现思路,具体的实现过程可能会因具体的需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值