zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    动态规划:POJ2576-Tug of War(二维费用的背包问题)
    动态规划:HDU3496-Watch The Movie(二维费用的背包问题)
    动态规划:HDU1712-ACboy needs your help(分组背包问题)
    水题:HDU1303-Doubles
    动态规划:HDU2844-Coins(多重背包的二进制优化)
    动态规划:HDU1059-Dividing(多重背包问题的二进制优化)
    动态规划:HDU1224-Free DIY Tour
    动态规划:HDU1864-最大报销额(处理带小数的dp问题)
    红黑树
    二叉树
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/5853665.html
Copyright © 2011-2022 走看看