iOS開發-RATreeView多層UITableViewCell展示【展開與收合】的使用,
1.前言
iOS開發時,經常接觸到的列表展示就是Tableview再熟悉不過了,但是如果接觸到多層多級cell的展示,用大牛Augustyniak寫的RATreeView是最好不過的了,Git地址:https://github.com/Augustyniak/RATreeView 閑話少說,重點在下面;
2.匯入RATreeView
如果安裝了CocoaPods,就直接在podfile檔案裡面添加 pod 'RATreeView', 儲存後直接終端輸入 pod install,就可以匯入到項目了,如果沒有安裝CocoaPods(這裡有CocoaPods的安裝參考),可以把RATreeView整個檔案夾拖入項目即可,小編建議最好用CocoaPods,易於維護;
3.RATreeView的使用
1.建立一個基於UITableViewCell的類RaTreeTableViewCell,同時勾選建立xib檔案
2.在RaTreeTableViewCell.h/m檔案裡添加自訂初始化方法:
.h
+ (instancetype)treeViewCellWith:(RATreeView *)treeView arrayCourse:(NSArray *)arrayCourse;
.m
+ (instancetype)treeViewCellWith:(RATreeView *)treeView arrayCourse:(NSArray *)arrayCourse{
RaTreeTableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:@"RaTreeTableViewCell"];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"RaTreeTableViewCell" owner:nil options:nil] firstObject];
}
///arrayCourse是我傳遞的參數模型
///_arrayCourseModel儲存傳遞模型
_arrayCourseModel = arrayCourse;
return cell;
}
3.在RaTreeTableViewCell.h/m檔案裡添加模型賦值方法(這個方法是最關鍵的):
.h
/**
title:cell上顯示的當前層級模型的標題
level:層級,這裡我用與判斷是否顯示上下收縮的箭頭 可以用 levelForCellForItem 方法擷取
children:當前層級下的位元組點個數
model:當前模型
*/
- (void)setCellValuesInfoWith:(NSString *)title level:(NSInteger)level children:(NSInteger )children courseModel:(CourseChaperModel *)model;
.m
- (void)setCellValuesInfoWith:(NSString *)title level:(NSInteger)level children:(NSInteger )children courseModel:(CourseChaperModel *)model{
_modelCourse = model;
//有子節點時顯示箭頭表徵圖
if (children==0) {
////self.iconView是我xib上添加的箭頭表徵圖
self.iconView.hidden = YES;
}
else { //否則不顯示
self.iconView.hidden = NO;
}
}
4.添加RATreeView
- (void)addRaTreeView{
_treeView = [[RATreeView alloc] initWithFrame:self.view.bound style:RATreeViewStylePlain];
__reeView.delegate = self;
_treeView.dataSource = self;
_treeView.separatorColor = [UIColor groupTableViewBackgroundColor];
_treeView.treeFooterView = [UIView new];
[self.view addSubview:_treeView];
// [_treeView reloadData]; ///重新整理方法
}
5.RATreeView代理實現
//delegate
//返回行高
- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item{
return 50;
}
//將要展開
- (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item{
}
//將要收縮
- (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item{
}
//已經展開
- (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 _arrayCourse是總的樹形結構的數組模型(模型中有數組模型)
RaTreeTableViewCell *cell = [RaTreeTableViewCell treeViewCellWith:treeView arrayCourse:_arrayCourse];
//當前item
CourseChaperModel *model = item;
//當前層級
NSInteger level = [treeView levelForCellForItem:item];
//賦值
[cell setCellValuesInfoWith:model.name level:level children:model.list2.count courseModel:model];
return cell;
}
/**
* 必須實現
*
* @param treeView treeView
* @param item 節點對應的item
*
* @return 每一節點對應的個數
*/
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item{
CourseChaperModel *model = item;
if (item == nil) {
return self.arrayCourse.count;
}
return model.list2.count;
}
/**
*必須實現的dataSource方法
*
* @param treeView treeView
* @param index 子節點的索引
* @param item 子節點索引對應的item
*
* @return 返回 節點對應的item
*/
- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item{
CourseChaperModel *model = item;
if (item==nil) {
return self.arrayCourse[index];
}
return model.list2[index];
}
//cell的點擊方法
- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item{
// //擷取當前的層
// NSInteger level = [treeView levelForCellForItem:item];
// //當前點擊的model
// CourseChaperModel *model = item;
}
//是否允許將要展開
//- (BOOL)treeView:(RATreeView *)treeView shouldExpandRowForItem:(id)item{
// return NO;
//}
///是否允許將要合上
//- (BOOL)treeView:(RATreeView *)treeView shouldCollapaseRowForItem:(id)item{
// return NO;
//}
////儲存格是否可以編輯 預設是YES
- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item{
return NO;
}
////編輯要實現的方法
//- (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item{
// NSLog(@"編輯了實現的方法");
//}
以上基礎的展示功能實現之後,可以在此基礎上控制控制項的展示以及cell複用時的一些操作 ,下面是我項目的效果: