博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS树状视图(折叠单元格)详细使用
阅读量:6428 次
发布时间:2019-06-23

本文共 5788 字,大约阅读时间需要 19 分钟。

RATreeView是一个第三方的iOS树视图(通俗的讲就是折叠单元格),它是对UITableView的封装,定义自己的委托和数据源的法,RATreeView是高度可定制的,并且有很多功能。很多朋友都说使用的不好,官方讲的也不够详细,所以我就给大家讲一下,怎么使用.


  • 首先先看下实现效果

  • 使用方法

CocoaPods pod 'RATreeView', '~> 2.1.0'

  • 具体使用 1.创建model
#import 
@interface RaTreeModel : NSObject@property (nonatomic,copy) NSString *name;//标题@property (nonatomic,strong) NSArray *children;//子节点数组//初始化一个model- (id)initWithName:(NSString *)name children:(NSArray *)array;//遍历构造器+ (id)dataObjectWithName:(NSString *)name children:(NSArray *)children;@end复制代码
#import "RaTreeModel.h"@implementation RaTreeModel- (id)initWithName:(NSString *)name children:(NSArray *)children{    self = [super init];    if (self) {        self.children = children;        self.name = name;    }    return self;}+ (id)dataObjectWithName:(NSString *)name children:(NSArray *)children{    return [[self alloc] initWithName:name children:children];}@end复制代码

2.创建cell

  • 特别注意 xib创建的cell, 一定不要勾选Use Auto Layout,否则cell上的布局不会执行.

找不到的,请看下图

cell创建具体如下:

这是我用xib设置的cell,你们按照你们的需求做

#import 
@interface RaTreeViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet UIImageView *iconView;//图标@property (weak, nonatomic) IBOutlet UILabel *titleLable;//标题//赋值- (void)setCellBasicInfoWith:(NSString *)title level:(NSInteger)level children:(NSInteger )children;@end复制代码
#import "RaTreeViewCell.h"@interface RaTreeViewCell ()@end@implementation RaTreeViewCell- (void)awakeFromNib {    // Initialization code    self.selectionStyle = UITableViewCellSelectionStyleNone;}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {    [super setSelected:selected animated:animated];    // Configure the view for the selected state}- (void)setCellBasicInfoWith:(NSString *)title level:(NSInteger)level children:(NSInteger )children{    //有自孩子时显示图标    if (children==0) {        self.iconView.hidden = YES;    }    else { //否则不显示        self.iconView.hidden = NO;    }    self.titleLable.text = title;    self.iconView.image = [UIImage imageNamed:@"close"];          //每一层的布局    CGFloat left = 10+level*30;        //头像的位置    CGRect  iconViewFrame = self.iconView.frame;        iconViewFrame.origin.x = left;        self.iconView.frame = iconViewFrame;        //title的位置    CGRect titleFrame = self.titleLable.frame;        titleFrame.origin.x = 40+left;        self.titleLable.frame = titleFrame;        }复制代码

3.创建RATreeView 只展示核心代码

######数据

//加载数据- (void)setData {        //宝鸡市 (四层)    RaTreeModel *zijingcun = [RaTreeModel dataObjectWithName:@"紫荆村" children:nil];        RaTreeModel *chengcunzheng = [RaTreeModel dataObjectWithName:@"陈村镇" children:@[zijingcun]];        RaTreeModel *fengxiang = [RaTreeModel dataObjectWithName:@"凤翔县" children:@[chengcunzheng]];    RaTreeModel *qishan = [RaTreeModel dataObjectWithName:@"岐山县" children:nil];    RaTreeModel *baoji = [RaTreeModel dataObjectWithName:@"宝鸡市" children:@[fengxiang,qishan]];        //西安市    RaTreeModel *yantaqu = [RaTreeModel dataObjectWithName:@"雁塔区" children:nil];    RaTreeModel *xinchengqu = [RaTreeModel dataObjectWithName:@"新城区" children:nil];        RaTreeModel *xian = [RaTreeModel dataObjectWithName:@"西安" children:@[yantaqu,xinchengqu]];        RaTreeModel *shanxi = [RaTreeModel dataObjectWithName:@"陕西" children:@[baoji,xian]];        [self.modelArray addObject:shanxi];}复制代码

#######代理方法

#pragma mark -----------delegate //返回行高- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item {     return 50;}//将要展开- (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item {        RaTreeViewCell *cell = (RaTreeViewCell *)[treeView cellForItem:item];    cell.iconView.image = [UIImage imageNamed:@"open"];   }//将要收缩- (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item {        RaTreeViewCell *cell = (RaTreeViewCell *)[treeView cellForItem:item];    cell.iconView.image = [UIImage imageNamed:@"close"];}//已经展开- (void)treeView:(RATreeView *)treeView didExpandRowForItem:(id)item {            NSLog(@"已经展开了");}//已经收缩- (void)treeView:(RATreeView *)treeView didCollapseRowForItem:(id)item {        NSLog(@"已经收缩了");}复制代码

####### dataSource方法

//返回cell- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item {            //获取cell    RaTreeViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"RaTreeViewCell"];        //当前item    RaTreeModel *model = item;        //当前层级    NSInteger level = [treeView levelForCellForItem:item];       //赋值    [cell setCellBasicInfoWith:model.name level:level children:model.children.count];       return cell;}/** *  必须实现 * *  @param treeView treeView *  @param item    节点对应的item * *  @return  每一节点对应的个数 */- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item{     RaTreeModel *model = item;         if (item == nil) {        return self.modelArray.count;    }    return model.children.count;}/** *必须实现的dataSource方法 * *  @param treeView treeView *  @param index    子节点的索引 *  @param item     子节点索引对应的item * *  @return 返回 节点对应的item */- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item {    RaTreeModel *model = item;    if (item==nil) {               return self.modelArray[index];    }       return model.children[index];}//cell的点击方法- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item {    //获取当前的层    NSInteger level = [treeView levelForCellForItem:item];    //当前点击的model    RaTreeModel *model = item;    NSLog(@"点击的是第%ld层,name=%@",level,model.name);}//单元格是否可以编辑 默认是YES- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item {        return YES;}//编辑要实现的方法- (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item {        NSLog(@"编辑了实现的方法");}复制代码

4.基本能用到的方法 我都写了注释,如有错误请指出.是不是比官方的清楚的多.

demo地址:

转载于:https://juejin.im/post/5a332f3ef265da431440bd75

你可能感兴趣的文章
深入浅出换肤相关技术以及如何实现
查看>>
Redis 基础、高级特性与性能调优
查看>>
React native 第三方组件 React native swiper
查看>>
接口幂等设计
查看>>
编程入门指南
查看>>
移动端的自适应方案—REM
查看>>
你真的懂volatile吗
查看>>
Android 编译时注解-提升
查看>>
说说 Spring AOP 中 @Aspect 的高级用法
查看>>
Workbox CLI中文版
查看>>
贝聊亿级数据库分库分表实践
查看>>
同时连接gitlab和github
查看>>
vuex源码分析
查看>>
香港智远:港股光伏板块中报期有望获资金青睐
查看>>
高峰论坛:专家热议物联网行业如何迎风飞扬
查看>>
企业实施大数据的三个问题和五大关键
查看>>
住建部解析智慧城市停车场建设三大关键问题
查看>>
德州仪器第四季度净利润8.36亿美元 同比增1%
查看>>
打造“天网工程”用“大数据”说话
查看>>
东莞发放光伏发电财政资金补助 莞企和居民都可以申请
查看>>