效果图
实现思路
-
主要部分的源码
controller
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView * tableView;
@property (nonatomic , strong) NSMutableArray * dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setData];
[self.view addSubview:self.tableView];
}
- (void)setData{
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 5; i++) {
LiModel *model = [[LiModel alloc] init];
model.index = [[NSString alloc] initWithFormat:@"%d",i];
model.isOpen = NO;
model.isMain = YES;
NSMutableArray *chArray = [[NSMutableArray alloc] init];
for (int k = 0; k < 3; k++) {
LiModel *model1 = [[LiModel alloc] init];
model1.index = [[NSString alloc] initWithFormat:@"%d-%d",i,k];
model1.isOpen = NO;
[chArray addObject:model1];
}
model.chArray = chArray;
[dataArray addObject:model];
}
self.dataArray = dataArray;
}
- (UITableView *)tableView{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor redColor];
}
return _tableView;
}
#pragma mark tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
LiModel *model = self.dataArray[indexPath.row];
if (model.isMain) {
cell1 * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
if (cell == nil) {
cell = [[cell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
}
cell.title.text = model.index;
return cell;
}else{
cell2 * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
if (cell == nil) {
cell = [[cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
}
cell.title.text = model.index;
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LiModel *model = self.dataArray[indexPath.row];
model.isOpen = !model.isOpen;
if (model.isOpen) {
for (int i = 0 ; i < model.chArray.count; i++) {
[self.dataArray insertObject:model.chArray[i] atIndex:indexPath.row + 1 + i];
}
}else{
for (int i = 0 ; i < model.chArray.count; i++) {
[self.dataArray removeObjectAtIndex:indexPath.row + 1];
}
}
[self.tableView reloadData];
}
model
@interface LiModel : NSObject
@property (nonatomic , strong) NSString *index;
@property (nonatomic , assign) BOOL isOpen;//是否展开
@property (nonatomic , assign) BOOL isMain;
@property (nonatomic , strong) NSArray<LiModel *>* chArray;//二级菜单的数据
@end
主要实现思路
-
展开操作
:将对应的一级model
下的二级model
添加到dataArray数组
中当前一级model所处下标的下一个位置
for (int i = 0 ; i < model.chArray.count; i++) {
[self.dataArray insertObject:model.chArray[i] atIndex:indexPath.row + 1 + i];
}
2.收起操作
:将dataArray数组中当前一级model下标的下一个位置
到一级model下标的下一个位置+一级model下的二级model.count
的所有数据移除掉
for (int i = 0 ; i < model.chArray.count; i++) {
[self.dataArray removeObjectAtIndex:indexPath.row + 1];
}
完整代码:github