zoukankan      html  css  js  c++  java
  • 详细解说仿制QQ列表 展开和收起列表

    近来公司的项目要写个类似于QQ列表展开和收起的列表,想到以前李明杰视频里说的,就写个了展开和收起的Demo,希望给新手一点思路,与大家共同进步

    我写的这个展开和收起的思路是,用一个字典的Key去记录点击了第几组,用状态0-1去记录他们是展开的还是收起的 

    废话不多说,上代码,注释已写,一定要认真看哦,认真看都能看懂

    #import "ViewController.h"
    
    
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    
    @interface ViewController () <UITableViewDataSource,UITableViewDelegate>
    @property (nonatomic,strong)UITableView *tabelView;
    @property (nonatomic,strong)NSMutableArray *array;
    @property (nonatomic,strong)NSMutableDictionary *dict;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建tableView
        self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        self.tabelView.delegate = self;
        self.tabelView.dataSource = self;
        [self.view addSubview:self.tabelView];
        //记录的字典
        self.dict = [NSMutableDictionary dictionary];
        //每组的标题
        self.array = [[NSMutableArray alloc] init];
        //去除tableView多余的横线
        self.tabelView.tableFooterView = [[UIView alloc] init];
        
        for(int i=0;i<5;i++){
            NSString *str = [NSString stringWithFormat:@"第%d组",i];
            [self.array addObject:str];
        }
    }
    //每组的组头
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        view.backgroundColor = [UIColor cyanColor];
        view.userInteractionEnabled = YES;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, (44-20)/2, 100, 20)];
        label.text = self.array[section];
        [view addSubview:label];
        //view的tag就等于section 代表点击了哪一个组
        view.tag = section;
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(display1:)]];
        return view;
    }
    - (void)display1:(UITapGestureRecognizer *)g{
        
        //将点击了哪一组转换成字符串
        NSString *str = [NSString stringWithFormat:@"%ld",g.view.tag];
        //从字典里面以第几组为key取出状态值
        //如果状态值为0,代表关闭
        if([self.dict[str] integerValue] == 0){
            [self.dict setObject:@(1) forKey:str];
        }
        //如果状态值为不为0,代表展开
        else{
            [self.dict setObject:@(0) forKey:str];
        }
        //记得一定要刷新tabelView,不然没有效果
        [self.tabelView reloadData];
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 44;
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 5;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        /*调用tableView的reloadData方法会重新调用这个方法
         从而从字典里面取出相应组对应的状态码,从而判断是需要展开还是收起
        */
        NSString *str = [NSString stringWithFormat:@"%ld",section];
        //将点击了哪一组转换成字符串
        if([self.dict[str] integerValue] == 1){
            //如果状态值为等于1,代表需要展开返回真正的多少个Cell
            return 5;
        }else{
            //如果状态值为等于0,代表需要收起返回0
            return 0;
        }
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellId = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        cell.textLabel.text = @"每一行";
        return cell;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    @end

    运行效果

    我在公司项目做的效果,我感觉后面的箭头还是做的不错的,哈哈,大家可以下载我们应用看看云收益Pro(iOS版本哦)


    谢谢大家观看

  • 相关阅读:
    List里如何剔除相同的对象?
    Collections工具类中的sort方法如何比较元素?
    TreeMap和TreeSet在排序时如何比较元素?
    Map的实现类中,哪些是有序的,哪些是无序的,如何保证其有序性?
    LinkedHashMap、LinkedHashSet、LinkedList哪个最适合当作Stack使用?
    ArrayList与LinkedList哪个插入性能高?
    HashSet和HashMap有什么区别?
    HashSet实现原理是什么?有什么特点?
    TreeSet的原理是什么?使用需要注意什么?
    Java中已经数组类型,为什么还要提供集合?
  • 原文地址:https://www.cnblogs.com/bcblogs/p/4881612.html
Copyright © 2011-2022 走看看