初始化header或footer

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView *footerView = [[UIView alloc] init];
//    footerView.backgroundColor = UIColorFromRGB(0xeff0f3);
    if(!_baseView){
        _baseView = [[UIView alloc]init];
        _baseView.backgroundColor = UIColorFromRGB(0xEBECF0);
        
        if(!_statusButton){
            _statusButton = [[UIButton alloc] init];
            [_statusButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_statusButton addTarget:self action:@selector(bottomStatusClicked) forControlEvents:UIControlEventTouchUpInside];
            _statusButton.layer.cornerRadius = 3;
            _statusButton.layer.masksToBounds = YES;
        }
        
        [_baseView addSubview:_statusButton];
        [_statusButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView.mas_left).offset(15);
            make.right.equalTo(self.baseView.mas_right).offset(-15);
            make.bottom.equalTo(self.baseView.mas_bottom).offset(-10);
            make.height.equalTo(@50);
        }];
    }
    
    return _baseView;
}

首先先判nilide

 

另一種寫法code

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = UIColorFromRGB(0xeff0f3);
        
    if(!_statusButton){
        [_statusButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_statusButton addTarget:self action:@selector(bottomStatusClicked) forControlEvents:UIControlEventTouchUpInside];
        _statusButton.layer.cornerRadius = 3;
        _statusButton.layer.masksToBounds = YES;
    }
    
    [footerView addSubview:_statusButton];
    [_statusButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView.mas_left).offset(15);
        make.right.equalTo(self.baseView.mas_right).offset(-15);
        make.bottom.equalTo(self.baseView.mas_bottom).offset(-10);
        make.height.equalTo(@50);
    }];
    
    
    return footerView;
}
相關文章
相關標籤/搜索