设为首页 加入收藏

TOP

iOS开发UI篇-实现tableView的层级显示(一)
2017-10-13 10:33:03 】 浏览:3511
Tags:iOS 开发 UI篇 实现 tableView 层级 显示

 进来要实现一个tableView 的cell层级显示,网上找的思路都各不相同.下面说一下我的实现思路.

 根据根标题存储cell的展开状态,添加到字典中.

 话不多说,直接上代码.

 

 


#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface CellTreesController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *MyTableView;

@property(nonatomic,strong)NSMutableArray *dataArray;//存放标题

@property(nonatomic,strong)NSMutableDictionary *boolDcitionary;//存放对应分区section是否展开

@end

@implementation CellTreesController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _MyTableView.dataSource = self;
    _MyTableView.delegate = self;
    _MyTableView.tableFooterView = [UIView new];
    [self.view addSubview:_MyTableView];
    
    //组头数据
    for (int i = 0; i < 20; i ++) {
        
        NSString *titleString = [NSString stringWithFormat:@"第%d组",i + 1];
        [self.dataArray addObject:titleString];
    }
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - dalegate dataSource

//区头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}

//区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 7, 150, 30)];
    lable.text = self.dataArray[section];
    lable.textColor = [UIColor orangeColor];
    headerView.tag = 1000 + section;
    //添加手势->控制cell的展开
    [headerView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickView:)]];
    [headerView addSubview:lable];
    return headerView;
}

//section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 20;
}

//row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *clickTag = [NSString stringWithFormat:@"%ld",section + 1000];
    
    if ([self.boolDcitionary[clickTag] integerValue] == 1) {
        return 6;
        
    }else{
        
        return 0;
    }
}

//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"myTableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row + 1];
    
    return cell;
    
}


#pragma mark - tap手势点击事件

- (void)clickView:(UITapGestureRecognizer *)action{
    
    NSString *clickTag = [NSString stringWithFormat:@"%ld",action.view.tag];

//第二个cell展开时仍然保持前一个cell的展开状态 //状态为0 代表关闭 if ([self.boolDcitionary[clickTag] integerValue] == 0) { [self.boolDciti
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS开发篇-申请开发者账号流程 下一篇关于UIView布局的总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目