#import "ViewController.h"
@interface ViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic,strong)UITableView *tableVIew;
@property(nonatomic,strong)NSMutableArray *arr;
@end
#define Name @"name"
#define List @"list"
#define Type @"type"
#define SUPerKey @"SUPerKey"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray * superNmae = @[@"AAAAA",@"BBBBBBBB",@"CCCCCCCCC",@"DDDDDDDD"].mutableCopy;
NSMutableArray * ziNmme = @[@"aaaaa",@"bbbbb",@"cccccc",@"dddddd"].mutableCopy;
NSMutableArray * sunNmme = @[@"5555",@"66666",@"77777",@"888888"].mutableCopy;
NSMutableArray * arr = [NSMutableArray array];
NSUInteger page = 1;
// 一级菜单
for (int i = 0; i < superNmae.count; i++)
{
NSMutableDictionary *dic = [self dicFromName:superNmae[i] superKey:page];
[arr addObject:dic];
}
page++;
// 二级菜单
[self addNewZiJi:arr ziNmme:ziNmme superKey:page];
page++;
// 三级菜单
for (int i = 0; i < arr.count; i++)
{
NSMutableArray * arrNew = arr[i][List];
[self addNewZiJi:arrNew ziNmme:sunNmme superKey:page];
}
self.arr = arr;
[self.view addSubview:self.tableVIew];
}
#pragma mark ----------------------- 添加子集字典 ------------------------
-(void)addNewZiJi:(NSMutableArray *)arr ziNmme:(NSMutableArray *)ziNmme superKey:(NSInteger)superKey
{
for (int i = 0; i < arr.count; i++)
{
NSMutableDictionary *dic = arr[i];
[dic setObject:[self addZi:ziNmme superName:dic[Name] superKey:superKey] forKey:List];
}
}
-(NSMutableArray *)addZi:(NSMutableArray *)subNmme superName:(NSString *)superName superKey:(NSInteger)superKey
{
NSMutableArray *arr = [NSMutableArray array];
for ( int j = 0; j < subNmme.count; j++ )
{
[arr addObject:[self dicFromName:subNmme[j] superKey:superKey]];
}
NSMutableDictionary *dic = [self dicFromName:superName superKey:superKey];
[dic setObject:arr forKey:List];
return arr;
}
#pragma mark ----------------------- 添加字典 ------------------------
-(NSMutableDictionary *)dicFromName:(NSString *)name superKey:(NSInteger)superKey
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
NSMutableArray *arr = [NSMutableArray array];
[dic setObject:arr forKey:List];
[dic setObject:name forKey:Name];
[dic setObject:@"1" forKey:Type];// 展开状态
[dic setObject:[NSString stringWithFormat:@"%zd",superKey] forKey:SUPerKey];
return dic;
}
-(UITableView *)tableVIew{
if (!_tableVIew) {
_tableVIew = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
_tableVIew.delegate = self;
_tableVIew.dataSource = self;
}
return _tableVIew;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]init];
// NSLog(@"2222 == %@",self.arr[indexPath.row]);
NSDictionary *dic = self.arr[indexPath.row];
NSLog(@"%@",[dic valueForKey:SUPerKey]);
NSInteger t = [[dic valueForKey:SUPerKey] integerValue];
NSString *str;
if (t == 0) {
str = @"";
}else if (t==1){
str = @" ";
}else if (t == 2){
str = @" ";
}else if (t == 3){
str = @" ";
}
cell.textLabel.text = [NSString stringWithFormat:@"%@%@",str,dic[Name]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dic = self.arr[indexPath.row];
[self cellManager:dic IndexPath:indexPath];
}
-(void)cellManager:(NSMutableDictionary *)dic IndexPath:(NSIndexPath *)indexPath{
NSMutableArray *arr = dic[List];
if (arr.count > 0)
{
NSString *str = dic[Type];
BOOL type = [str integerValue] == 0?NO:YES;
if (type)
{
for (NSDictionary *dic in arr)
{
[self.arr insertObject:dic atIndex:indexPath.row + 1];
}
}
else
{
for (int i = 0; i <arr.count ; i++) {
[self.arr removeObjectAtIndex:indexPath.row+1];
}
}
type = !type;
[dic setValue:[NSString stringWithFormat:@"%zd",type] forKey:Type];
}
[self.tableVIew reloadData];
}
-(NSMutableArray *)arr{
if (!_arr) {
_arr = [NSMutableArray array];
}
return _arr;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS 简单实现树形结构列表
猜你喜欢
转载自blog.csdn.net/saw471/article/details/81044890
今日推荐
周排行