tableView区头折叠

作者: 男人宫 | 来源:发表于2018-01-15 16:05 被阅读14次
    • ZHZHeaderView.h文件
    
    @class ZHZHeaderView;
    /** *  协议 *  作用: 传值(点击区头的section 和 区头view) *  
    @param section  区头的section * 
     @param headview 区头view */
    @protocol HeaderProtocol
    - (void)clickHeaderOfSection:(NSInteger)section headView:(ZHZHeaderView *)headview;
    @end
    @interface ZHZHeaderView : UITableViewHeaderFooterView
    @property (nonatomic, assign)id delegate;
    
    /**
    
    *  添加 轻击 手势
    
    */
    
    @property (nonatomic, strong)UITapGestureRecognizer *tap;
    
    @property (nonatomic, strong)UIImageView *imageV;
    
    @property (nonatomic, strong)UILabel *titleLB;
    
    @property (nonatomic, strong)UIImageView *typeImagV;
    
    @end
    
    
    • ZHZHeaderView.m文件
    
    - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
    
    {
    
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
    
    //在这里向contentView添加控件
    
    _imageV = [[UIImageView alloc] init];
    
    [self.contentView addSubview:_imageV];
    
    _titleLB = [[UILabel alloc] init];
    
    [self.contentView addSubview:_titleLB];
    
    _typeImagV = [[UIImageView alloc] init];
    
    [self.contentView addSubview:_typeImagV];
    
    }
    
    return self;
    
    }
    
    //布局子控件时添加 tap手势
    
    - (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.imageV.frame = CGRectMake(40, 5, 25, 30);
    
    self.imageV.backgroundColor = [UIColor redColor];
    
    self.titleLB.frame = CGRectMake(70, 5, 100, 30);
    
    self.titleLB.backgroundColor = [UIColor greenColor];
    
    self.typeImagV.frame = CGRectMake(SCREEN_WIDTH - 70, 5, 30, 30);
    
    self.typeImagV.backgroundColor = [UIColor greenColor];
    
    //    self.contentView.backgroundColor = [UIColor redColor];
    
    [self.contentView addGestureRecognizer:self.tap];
    
    }
    
    #pragma mark - Action
    
    - (void)tapAction:(UITapGestureRecognizer *)tap {
    
    if ([self.delegate respondsToSelector:@selector(clickHeaderOfSection:headView:)]) {
    
    [self.delegate clickHeaderOfSection:self.tag headView:self];
    
    }
    
    }
    
    #pragma mark - Getter
    
    - (UITapGestureRecognizer *)tap {
    
    if (_tap == nil) {
    
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    }
    
    return _tap;
    
    }
    
    #pragma mark - life
    
    - (void)dealloc
    
    {
    
    [self removeGestureRecognizer:self.tap];
    
    }
    
    
    • tableview的代码块
    
    #import "PhysicalViewController.h"#import "ZHZHeaderView.h"@interface PhysicalViewController ()@property (nonatomic, strong)  UITableView  *scrollerTableV;  //整个屏幕大的tableView
    
    @property (nonatomic, strong)  UIView      *headerclassView; //集合View
    
    ///数据字典
    
    @property (nonatomic, strong)NSDictionary *cityDict;
    
    ///存放每个区域状态
    
    @property (nonatomic, strong)NSMutableArray *foldArr;
    
    @end
    
    @implementation PhysicalViewController
    
    //懒加载创建tableView
    
    -(UITableView *)scrollerTableV
    
    {
    
    if (!_scrollerTableV) {
    
    _scrollerTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    
    _scrollerTableV.backgroundColor = KBackColor;
    
    //        [_scrollerTableV registerClass:[HomePageCell class] forCellReuseIdentifier:@"HotTopicCell"];
    
    //
    
    //        [_scrollerTableV registerClass:[HomePageCell class] forCellReuseIdentifier:@"HomePageCell"];
    
    [_scrollerTableV registerClass:[ZHZHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    
    //去掉cell默认的分割线
    
    //        _scrollerTableV.separatorStyle = NO;
    
    _scrollerTableV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    }
    
    return _scrollerTableV;
    
    }
    
    - (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"物理";
    
    self.view.backgroundColor = [UIColor greenColor];
    
    self.foldArr = [NSMutableArray array];
    
    for (int i = 0; i <5; i++) {
    
    [self.foldArr addObject:@0];
    
    }
    
    [self.view addSubview:self.scrollerTableV];
    
    _scrollerTableV.delegate = self;
    
    _scrollerTableV.dataSource = self;
    
    [self setHeaderView];
    
    }
    
    //头部视图
    
    - (void)setHeaderView{
    
    _scrollerTableV.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    
    _scrollerTableV.tableHeaderView.backgroundColor = [UIColor whiteColor];
    
    _headerclassView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    
    _headerclassView.backgroundColor = [UIColor blueColor];
    
    [_scrollerTableV.tableHeaderView addSubview:_headerclassView];
    
    [self setThreeGradButton];
    
    }
    
    - (void)setThreeGradButton
    
    {
    
    for (int i = 0; i <3 ; i ++) {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(40+((SCREEN_WIDTH-240)/3 +80)*i, 20, (SCREEN_WIDTH-240)/3, 100-40);
    
    button.tag = i + 1;
    
    [button setBackgroundColor:[UIColor redColor]];
    
    [button addTarget:self action:@selector(gradButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_headerclassView addSubview:button];
    
    }
    
    }
    
    - (void)gradButtonClick:(UIButton *)button
    
    {
    
    NSLog(@"点击的按钮是:%ld",button.tag);
    
    }
    
    #pragma mark---tableviewDelegate
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    
    {
    
    return 5;
    
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
    {
    
    if ([self.foldArr[section] integerValue]) {
    
    return 5;
    
    }
    
    return 0;
    
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
    return 50;
    
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    
    {
    
    return 45;
    
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
    
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    
    cell.textLabel.text = @"声现象";
    
    }
    
    return cell;
    
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    ZHZHeaderView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    
    headView.titleLB.text = @"小学生啊";
    
    headView.tag = section;
    
    headView.delegate = self;
    
    return headView;
    
    }
    
    #pragma mark -HeaderProtocol
    
    - (void)clickHeaderOfSection:(NSInteger)section headView:(ZHZHeaderView *)headview{
    
    NSLog(@"点击了区头");
    
    self.foldArr[section] = @(![self.foldArr[section] integerValue]);
    
    NSLog(@"%@",self.foldArr[section]);
    
    [_scrollerTableV reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:tableView区头折叠

        本文链接:https://www.haomeiwen.com/subject/twkdoxtx.html