iOS TableView折叠

一如既往的精简不啰嗦,直接上代码,如有问题可以给我留言.

1,创建控制器 :

   ViewController.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

  ViewController.m文件

#import "ViewController.h"
#import "AKTableViewFold.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    NSArray *cellArray = @[@{@"sectionName":@"用户管理",
                             @"sectionArray":@[@"岗位配置",@"人员配置"]},
                           @{@"sectionName":@"流程管理",
                             @"sectionArray":@[@"流程管理"]},
                           @{@"sectionName":@"物资管理",
                             @"sectionArray":@[@"物资管理"]},
                           @{@"sectionName":@"公司管理",
                             @"sectionArray":@[@"修改公司信息",@"同级公司管理"]}];
    AKTableViewFold *foldCell = [[AKTableViewFold alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    foldCell.tableArray = cellArray;
    [self.view addSubview:foldCell];
}

@end

2,新建TableView类:

   AKTableViewFold.h文件

#import <UIKit/UIKit.h>

@interface AKTableViewFold : UITableView<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSArray *tableArray;

@end

  AKTableViewFold.m文件

#import "AKTableViewFold.h"

@interface AKCellHeaderVeiw : UITableViewHeaderFooterView

@property (nonatomic,copy) NSString *sectionTitle;
@property (nonatomic,strong) UILabel *sectionLabel;
@property (nonatomic,strong) UIButton *foldBtn;
@property (nonatomic,copy) void(^clickBtn)(UIButton *);

@end
@implementation AKCellHeaderVeiw

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self){
        
        _sectionLabel = [[UILabel alloc]init];
        _foldBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_foldBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:_sectionLabel];
        [self.contentView addSubview:_foldBtn];
    }
    
    return self;
}

- (void)clickButton:(UIButton *)btn{
    
    if (_clickBtn){
        
        self.clickBtn(btn);
    }
    
    NSLog(@"UOU");
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    _sectionLabel.frame = CGRectMake(10, 0, 100, 50);
    _foldBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 60, 0, 50, 50);
    
}

-(void)setSectionTitle:(NSString *)sectionTitle{
    
    _sectionTitle = sectionTitle;
    _sectionLabel.text = sectionTitle;
}

@end

@interface AKTableViewFold ()

@property (nonatomic,strong) NSMutableArray *remeberArray;

@end
@implementation AKTableViewFold

- (NSArray *)remeberArray{
    
    if (!_remeberArray){
        _remeberArray = [NSMutableArray array];
        for (int i = 0;i<_tableArray.count;i++){
            
            [_remeberArray addObject:@(0)];
        }
    }
    return _remeberArray;

}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    
    self = [super initWithFrame:frame style:style];
    if (self){
        
        
        [self registerClass:[AKCellHeaderVeiw class] forHeaderFooterViewReuseIdentifier:@"HEADVIEW"];
        [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELLKUTURE"];
        
        self.delegate = self;
        self.dataSource = self;
    }
    
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _tableArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if ([_remeberArray[section] integerValue] == 1){
        
        return [_tableArray[section][@"sectionArray"] count];
        
    }else{
        
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    AKCellHeaderVeiw *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HEADVIEW"];
    headView.sectionTitle = _tableArray[section][@"sectionName"];
        __weak typeof(self) weakSelf = self;
    headView.clickBtn = ^(UIButton *btn){
        
        weakSelf.remeberArray[section] = @(![weakSelf.remeberArray[section] integerValue]);
        
               [tableView reloadData];
    };
    
    
    if ([weakSelf.remeberArray[section] integerValue]){
        
        [headView.foldBtn setImage:[UIImage imageNamed:@"offline_clarity_green_up_iphone"] forState:UIControlStateNormal];
        
    }else if (![weakSelf.remeberArray[section] integerValue]){
        
        [headView.foldBtn setImage:[UIImage imageNamed:@"offline_clarity_green_down_iphone"] forState:UIControlStateNormal];
    }

    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLKUTURE"];
    
    cell.textLabel.text = _tableArray[indexPath.section][@"sectionArray"][indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

@end

 

转载于:https://my.oschina.net/Kuture/blog/733555

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值