美文网首页
iOS UITableview中cell的下拉展开

iOS UITableview中cell的下拉展开

作者: Sunny_Fight | 来源:发表于2017-06-22 17:19 被阅读39次

    前天做的一个小Demo,在产品需求中比较常见,这不就抽出来做了一个简版的,废话不多说直接上效果图,就是这么粗暴直接!O(∩_∩)O

    效果图

    效果图.gif

    Model层

    用的是假数据手写的plist文件,然后使用kvc的方式将数据对象话,并且model层中添加一个BOOL属性来标示对应的section是否选中

    plist文件截图.png

    Model层 .m文件代码如下:

    @implementation sectionModelGroup
    /**
     KVC
     */
    -(sectionModelGroup *)initWithDictionary:(NSDictionary *)dic {
        if (self = [super init]) {
            //数据对象化
            [ self setValuesForKeysWithDictionary:dic];
        }
        return self;
    }
    /**
     创建一个类方法,方便调用
     */
    +(sectionModelGroup *)creatSectionModelWithDictionary:(NSDictionary *)dic {
        return  [[self alloc]initWithDictionary:dic];
    }
    @end
    

    Controller层

    主要的逻辑:

    1.获取model层中数据,在numberOfRows中根据选中属性来返回对应的行数,YES则返回正确行数,NO则返回0行。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        sectionModelGroup *model = self.modelArray[section];
       
        if (model.isSelected) {
             return model.rows.count;
        }
        return 0;
    }
    

    2.给每个section加一个header,并添加button,点击事件是重新刷新对应section的数据,中间用tag值来标示对应的button

    /**
     button的点击事件
     */
    - (void)clickButton :(UIButton *)sender  {
        sectionModelGroup *model = self.modelArray[sender.tag -1000];
        
        model.isSelected = !model.isSelected;
      //重新载入对应section的数据(如果为为yes则返回实际行数,如果为no则返回0行)
            for (int i = 0; i < self.modelArray.count; i++) {
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag -1000] withRowAnimation:UITableViewRowAnimationFade];
            
            
        }
        
    }
    

    作为一个处女座,是非常追求完美的,非常不喜欢使用tag值,非常不喜欢使用tag值,非常不喜欢使用tag值,重要的事情说三遍!!!目前正在想解决办法规避tag值。
    抽时间弄个github,把这些小Demo上传上去,存到硬盘里面太乱了,心塞...

    相关文章

      网友评论

          本文标题:iOS UITableview中cell的下拉展开

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