折叠单元格的样式如图:
昨天完成使用第三方RATreeView实现折叠效果,组长说要把怎么使用的过程详细的记录下来以便以后用到很快上手,我觉得很有道理.所以你们看到了现在的它
RATreeView是对TableView的一成封装,和TableView的使用样式几乎一模一样
首先要pod RATreeView
下载下来之后不着急,先做个数据模型出来,这个数据模型有一点需要大家注意,必须有一个子节点数组属性,不然就没有使用这个第三方的理由了,为什么呢?因为我们展示的树状结构就是一个节点套另一个节点,如果不需要嵌套直接使用UItableView就可以展示了.回归到代码
#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
完成了模型
那么接下来我们来做用来展示的cell,在这里我直接使用xib了,使用xib的一个注意点:就是不要勾选 Use Auto Layout,不然cell的布局会不起作用
使用xib创建的cell样式,可以根据自己的需求来改变cell的样式
#import <UIKit/UIKit.h>
#import <RATreeView.h>
@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;
+ (instancetype)treeViewCellWith:(RATreeView *)treeView;
@end
#import "RaTreeViewCell.h"
@interface RaTreeViewCell()
@end
@implementation RaTreeViewCell
+ (instancetype)treeViewCellWith:(RATreeView *)treeView
{
RaTreeViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"RaTreeViewCell"];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"ModelCell" owner:nil options:nil] firstObject];
}
return cell;
}
- (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;
}
@end
cell创建完毕 最后一步完成RATreeView 在一开始就和大家介绍了说,RATreeView的用法和UITableView的用法特别的类似
#import "ViewController.h"
#import "RaTreeModel.h"
#import "RaTreeViewCell.h"
#import <RATreeView.h>
@interface ViewController ()<RATreeViewDataSource, RATreeViewDelegate>
@property (nonatomic, strong) NSMutableArray *modelArray;
@end
@implementation ViewController
//创建RATreeView 并赋值给控制器的view
- (void)loadView
{
RATreeView *ratreeView = [[RATreeView alloc] initWithFrame:[UIScreen mainScreen].bounds style:RATreeViewStylePlain];
ratreeView.delegate = self;
ratreeView.dataSource = self;
self.view = ratreeView;
self.view.backgroundColor = [UIColor whiteColor];
[self setData];
}
- (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 = [NSMutableArray array];
[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 = [RaTreeViewCell treeViewCellWith:treeView];
//当前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(@"编辑了实现的方法");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
记住不要忘了设置数据源和代理 就这么搞定了