先看效果
先上代碼吓揪,在講思路!所计!
創(chuàng)建tableView
-(UITableView *)table{
if (!_table) {
_table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_table.delegate = self;
_table.dataSource = self;
_table.tableFooterView = [UIView new];
[myCell registerTableViewWith:_table];
}
return _table;
}
創(chuàng)建總的數(shù)據(jù)模型柠辞,后面只操作這一個(gè)模型數(shù)組
-(NSMutableArray *)dataArr{
if (!_dataArr) {
_dataArr = @[].mutableCopy;
}
return _dataArr;
}
模型結(jié)構(gòu)
@interface ListModel : NSObject
@property (nonatomic ,copy) NSString *name;
///是否展開收起
@property (nonatomic ,assign) BOOL isOpen;
///子節(jié)點(diǎn)
@property (nonatomic ,strong) NSMutableArray *array;
@property (nonatomic ,assign) int leave;
@end
模擬數(shù)據(jù),通過leave來(lái)區(qū)別是第幾級(jí)數(shù)據(jù):0是一級(jí)數(shù)據(jù),1是二級(jí)數(shù)據(jù)醉箕,2是三級(jí)數(shù)據(jù)
-(void)initData{
for (int i = 0; i < 10; i++) {
ListModel *model = [[ListModel alloc] init];
model.name = [NSString stringWithFormat:@"%d", I];
model.array = @[].mutableCopy;
model.leave = 0;
for (int j = 0; j < 3; j++) {
ListModel *sonModel = [[ListModel alloc] init];
sonModel.leave = 1;
sonModel.array = @[].mutableCopy;
sonModel.name = [NSString stringWithFormat:@" j-%d",j];
[model.array addObject:sonModel];
for (int p = 0; p < 4; p++) {
ListModel *nextModel = [[ListModel alloc] init];
nextModel.leave = 2;
nextModel.name = [NSString stringWithFormat:@" p-%d",p];
[sonModel.array addObject:nextModel];
}
}
[self.dataArr addObject:model];
}
[self.table reloadData];
}
自定義cell
@interface myCell : UITableViewCell
+(NSString *)identifier;
+(void)registerTableViewWith:(UITableView *)table;
@property (nonatomic ,strong) UILabel *nameLab;
@property (nonatomic ,strong) UILabel *fLab;
@end
@implementation myCell
-(UILabel *)nameLab{
if (!_nameLab) {
_nameLab = [[UILabel alloc] initWithFrame:CGRectMake(100, 14, 100, 25)];
}
return _nameLab;
}
-(UILabel *)fLab{
if (!_fLab) {
_fLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 40, 25)];
_fLab.text = @"展開";
}
return _fLab;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.nameLab];
[self.contentView addSubview:self.fLab];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
+(NSString *)identifier{
return NSStringFromClass([self class]);
}
+(void)registerTableViewWith:(UITableView *)table{
[table registerClass:[self class] forCellReuseIdentifier:[self identifier]];
}
@end
核心代碼
#pragma mark tableView的代理
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
myCell *cell = [tableView dequeueReusableCellWithIdentifier:[myCell identifier]];
ListModel *model = self.dataArr[indexPath.row];
cell.nameLab.text = model.name;
if (model.leave == 1) {
cell.backgroundColor = UIColor.redColor;
cell.fLab.frame = CGRectMake(25, 14, 40, 25);
}
else if (model.leave == 2){
cell.backgroundColor = UIColor.brownColor;
cell.fLab.frame = CGRectMake(35, 14, 40, 25);
}
else{
cell.backgroundColor = UIColor.whiteColor;
cell.fLab.frame = CGRectMake(10, 14, 40, 25);
}
return cell;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArr.count;
}
tableViewCell 點(diǎn)擊方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
myCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ListModel *model = self.dataArr[indexPath.row];
model.isOpen = !model.isOpen;
self.rowCount = indexPath.row;
if (model.isOpen) {
[self insertDataArr:model.array];
cell.fLab.text = @"收起";
}else{
//刪除添加的model
[self deleteRowWith:model.array deleteRow:indexPath.row];
cell.fLab.text = @"展開";
}
}
///插入cell
-(void)insertDataArr:(NSMutableArray *)array{
NSMutableArray *arr = [NSMutableArray array];
[self insertModelArr:array resultArray:arr];
[self.table beginUpdates];
[self.table insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
[self.table endUpdates];
}
-(void)insertModelArr:(NSMutableArray *)array resultArray:(NSMutableArray *)resultArr{
for (int i = 0; i < array.count; i++) {
ListModel *model = array[I];
self.rowCount++;
[self.dataArr insertObject:model atIndex:self.rowCount];
NSIndexPath *index = [NSIndexPath indexPathForRow:self.rowCount inSection:0];
[resultArr addObject:index];
if (model.isOpen) {
[self insertModelArr:model.array resultArray:resultArr];
}
}
}
///刪除cell
-(void)deleteRowWith:(NSMutableArray *)array deleteRow:(NSInteger)row{
NSMutableArray *arr = [NSMutableArray array];
[self deleteRow:array deleteRow:row resultArray:arr];
[self.table beginUpdates];
[self.table deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic];
[self.table endUpdates];
}
-(void) deleteRow:(NSMutableArray *)array deleteRow:(NSInteger)row resultArray:(NSMutableArray *)resultArr{
for (int i = 0; i < array.count; i++) {
ListModel *model = array[I];
[self.dataArr removeObject:model];
++row;
NSIndexPath *index = [NSIndexPath indexPathForRow:row inSection:0];
[resultArr addObject:index];
if (model.isOpen == YES) {
[self deleteRow:model.array deleteRow:row resultArray:resultArr];
NSIndexPath *index = resultArr.lastObject;
row = index.row;
}
}
}
思路:
點(diǎn)擊哪個(gè)cell 就把對(duì)用的模型 放到 self.dataArr 這個(gè)數(shù)組中,然后刷新列表達(dá)到展開效果徙垫,收起的話也是通過刪除對(duì)應(yīng)的模型再刷新列表讥裤。