自定义tableview的footerview

之前一直没有自定义过tableview的footerview,猛的,汪同学让我自定义下,还有点弱弱的,本来想问他的,后来觉得应该也是比较简单的。遂自己查了下,果然很简单。记录下,以备后续。

header通过下面两个代理方法设置

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

footer通过下面两个

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

如果要做整个tableview的header和footer,要通过tableview setHeaderView setFooterView

实例:

在tableview中的设置:

 addTableViewFootView *view = [[addTableViewFootView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
        //view.backgroundColor = [UIColor redColor];
        view.delegate = self;
        [_myTableView setTableFooterView:view];

在tableview中实现代理方法:

(1)首先实现代理协议

(2)实现代理方法

-(void)myTabVAddClick:(UIButton *)btn
{

    //1.点击的是全选按钮
    if ([btn.titleLabel.text isEqualToString:@"全选"]) {
        int m = 0;
        int n = 0;
        NSString * flag;
        
        for(int i = 0;i =0 && n>0) {
            flag = @"Y";
           for(int i = 0;i 




自定义的view:addTableviewFootView的头文件和.m文件

#import 


@protocol myTabAddVdelegate 

-(void)myTabVAddClick:(UIButton *)btn;
@end

@interface addTableViewFootView : UIView


@property (nonatomic,strong) UIButton * cellSelectBtn;
@property (nonatomic,strong) UIButton * addBtn;

@property(assign,nonatomic)iddelegate;




@end
.m文件

#import "addTableViewFootView.h"

@implementation addTableViewFootView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1.全部选中或者全部不选中
        _cellSelectBtn = [[UIButton alloc]init];
        _cellSelectBtn.backgroundColor = BTNBACKGROUNDCOLOR;
        _cellSelectBtn.layer.masksToBounds = YES;
        _cellSelectBtn.layer.cornerRadius = 5;
        _cellSelectBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        
        [self addSubview:_cellSelectBtn];
        
        [_cellSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            
            make.centerY.equalTo(self.mas_centerY);
            make.height.mas_equalTo(44);
            make.width.mas_equalTo(140);
            
        }];
        
        
        //2.确定加入分组
        _addBtn = [[UIButton alloc]init];
        _addBtn.backgroundColor = BTNBACKGROUNDCOLOR;
        _addBtn.layer.masksToBounds = YES;
        _addBtn.layer.cornerRadius = 5;
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [self addSubview:_addBtn];
        [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10);
            make.centerY.equalTo(self.mas_centerY);
            make.width.mas_equalTo(140);
            make.height.mas_equalTo(44);
        }];
        [_addBtn setTitle:@"加入分组" forState:UIControlStateNormal];
        [_cellSelectBtn setTitle:@"全选" forState:UIControlStateNormal];
        
        [_cellSelectBtn addTarget:self action:@selector(btnclickedSelect) forControlEvents:UIControlEventTouchUpInside];
        [_addBtn addTarget:self action:@selector(btnclicked) forControlEvents:UIControlEventTouchUpInside];
        

    }
    return self;
}



- (void)awakeFromNib {
    // Initialization code
}


-(void)btnclickedSelect{
    
    NSLog(@"全选按钮被点击");
    [self.delegate myTabVAddClick:_cellSelectBtn];
    
}
-(void)btnclicked
{
    NSLog(@"加入分组按钮被点击");
    [self.delegate myTabVAddClick:_addBtn];
}




你可能感兴趣的:(mac,ios)