Demo - TableView第一个section的headerView添加刷新按钮

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 36)];
    headV.backgroundColor = [UIColor whiteColor];
    UILabel *text = [[UILabel alloc] init];
    text.font = [UIFont systemFontOfSize:15];
    text.textColor = [UIColor blackColor];
    [headV addSubview:text];
    [text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@16);
        make.left.equalTo(headV).offset(15);
        make.centerY.equalTo(headV);
        make.width.equalTo(headV).multipliedBy(0.5);
    }];
    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 35.5, kScreenW, 0.5)];
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.5;
    [headV addSubview:lineV];
    switch (section) {
        case 0:{
            text.text = @"论坛";
            [self initReloadBtnWithSuperView:headV];
            break;
        }
        case 1:{
            text.text = @"通知";
            break;
        }
        default:
            break;
    }
    return headV;
}
- (void)initReloadBtnWithSuperView:(UIView *)headV{
    UIButton *reloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [reloadBtn setBackgroundImage:[UIImage imageNamed:@"ununited_btn"] forState:UIControlStateNormal];
    [reloadBtn setTitle:@"刷新" forState:UIControlStateNormal];
    reloadBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [reloadBtn setTitleColor:[UIColor colorWithRed:(30 / 255.0) green:(30 / 255.0) blue:(30 / 255.0) alpha:1.0] forState:UIControlStateNormal];
    [reloadBtn addTarget:self action:@selector(reloadBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [headV addSubview:reloadBtn];
    self.reloadBtn = reloadBtn;
    [reloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 24));
        make.centerY.equalTo(headV);
        make.right.equalTo(headV).offset(-20);
    }];
    
    self.indicatorV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [headV addSubview:self.indicatorV];
    [self.indicatorV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(reloadBtn);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
}
//刷新按钮点击
- (void)reloadBtnDidClick:(UIButton *)btn{
    btn.hidden = YES;
    [self.indicatorV startAnimating];
    [self requestNoticeList];
    [self requestPostDirList];
}

//开始刷新
- (void)startReload{
    self.reloadBtn.hidden = YES;
    [self.indicatorV startAnimating];
}

//结束刷新
- (void)stopReload{
    [self.indicatorV stopAnimating];
    self.reloadBtn.hidden = NO;
}

你可能感兴趣的:(Demo - TableView第一个section的headerView添加刷新按钮)