在使用RATreeView
三方中其他的感觉没有太大的问题 包括数据的增删改查,下拉动画等等,但是最近使用RATreeView做项目,项目要求树形结构默认展开第一层,RATreeView默认却是收起的,无奈了之后,我就开始在这个RATreeView三方的源文件到处寻找 ,希望可以找到RATreeView提供给外界的API
,当然了 结果是比较失望的,但是功夫不负有心人,问题终于在这里找个答案了 https://github.com/Augustyniak/RATreeView/issues/122
首先大致分析一下源文件:
RATreeView
: 树形结构主体页面
RATreeView+TableViewDelegate
: RATreeView的分类代理,目的是为了和RATableView绑定
RATableView
: RATreeView的内置tab
RATreeNode
:包含了item和expanded ,item相当于model expanded控制展开收起
主体代码
- 创建treeTab
- (RATreeView *)treeTab {
if (!_treeTab) {
_treeTab = [[RATreeView alloc] init];
_treeTab.backgroundColor = [UIColor whiteColor];
_treeTab.frame= CGRectMake(0, 0, ScreenW, TreeViewH);
_treeTab.delegate = self;
_treeTab.dataSource = self;
_treeTab.separatorColor = BACKCOLOUR;
[_treeTab registerNib:[UINib nibWithNibName:@"RaTreeViewCell" bundle:nil] forCellReuseIdentifier:@"RaTreeViewCell"];
}
return _treeTab;
}
- 设置数据
-(void)setArrData:(NSMutableArray *)arrData
{
self.arr = @[].mutableCopy;
_arrData = arrData;
// 此处是构建model 然后加入数组
[self checkParent:arrData];
[_treeTab reloadData];
--------------默认展开第几层-------------------------------------------------------
for (id item in [_treeTab itemsForRowsInRect:_treeTab.frame])
{
[_treeTab expandRowForItem:item expandChildren:NO withRowAnimation:RATreeViewRowAnimationNone];
}
_treeTab.frame= CGRectMake(0, -TreeViewH, ScreenW, TreeViewH);
}
- 代理
#pragma mark --- delegate
- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item {
return 50;
}
- (BOOL)treeView:(RATreeView *)treeView shouldExpandRowForItem:(id)item
{
BOOL expanded = [treeView isCellForItemExpanded:item];
return YES;
}
- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item {
//获取cell
RaTreeViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"RaTreeViewCell"];
//当前item
MonitorTreeModel *model = item;
cell.delegate = self;
//当前层级
NSInteger level = [treeView levelForCellForItem:item];
//赋值
[cell setCellBasicInfoWith:model.comName level:level children:model.children.count];
if (model.isNoExpand == 0) {
cell.iconView.image = [UIImage imageNamed:@"TreeAdd"];
}else{
cell.iconView.image = [UIImage imageNamed:@"TreeDeduct"];
}
if (model.children.count ==0)
{
cell.iconView.image = IMG(@"TreePoint");
}
return cell;
}
/**
* 必须实现
* @param treeView treeView
* @param item 节点对应的item
* @return 每一节点对应的个数
*/
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
MonitorTreeModel *model = item;
if (item == nil) {
return self.arr.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 {
MonitorTreeModel *model = item;
if (item==nil) {
return self.arr[index];
}
return model.children[index];
}
//cell的点击方法
- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item {
RaTreeViewCell *cell = (RaTreeViewCell *)[treeView cellForItem:item];
//当前点击的model
MonitorTreeModel *model = item;
model.isNoExpand = model.isNoExpand==0 ? 1 : 0;
if (model.isNoExpand == 0) {
cell.iconView.image = [UIImage imageNamed:@"TreeAdd"];
}else{
cell.iconView.image = [UIImage imageNamed:@"TreeDeduct"];
}
if (model.children.count ==0)
{
cell.iconView.image = IMG(@"TreePoint");
}
}
//单元格是否可以编辑 默认是YES
- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item {
return NO;
}
- 设置cell
//点击cell的按钮
- (IBAction)ClickDetail:(UIButton *)sender
{
[self.delegate ClickWithCell:self];
}
// 图标 文本的显示
- (void)setCellBasicInfoWith:(NSString *)title level:(NSInteger)level children:(NSInteger )children{
self.titleLable.text = title;
//每一层的布局
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;
}
网友评论