1. 程式人生 > >自定義tableview的footerview

自定義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 <self.selected.count;i++){
            flag = self.selected[i];
            if ([flag isEqualToString:@"Y"]) {
                //flag = @"N";
                m++;
            }else{
                //flag = @"Y";
                n++;
            }
          
        }
        
        //如果選中了一些人,然後想全選
        if (m >=0 && n>0) {
            flag = @"Y";
           for(int i = 0;i <self.selected.count;i++){
               self.selected[i] = flag;
            }
        }
        if (n == 0) {
            flag = @"N";
            for(int i = 0;i <self.selected.count;i++){
                self.selected[i] = flag;
            }
        }
        
        
        //重新整理介面
        [_myTableView reloadData];
    }
    //2.點選的是加入分組按鈕---就將對應的人加入到對應的組中去
    else{
        //(1)判斷分組名是否存在,存在才能插入
        if(_groupTitleString.length <=0){
        
            _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            _hud.mode = MBProgressHUDModeText;
            _hud.labelText = @"您還沒有輸入分組名";
            _hud.margin = 10.f;
            _hud.removeFromSuperViewOnHide = YES;
            [_hud hide:YES afterDelay:1];
            return;
        }
        
        //(2)將選中的人 的分組欄位更新
        AppDelegate *myAppDelegate = [[UIApplication sharedApplication]delegate];
        if ([myAppDelegate.db open]) {
            //開啟成功
            //記錄加入組中的人數
            int count = 0;
            
            //查詢該組在資料庫中的id
            NSString *ID;
            if ([self.flag isEqualToString:@"add"]) {
                FMResultSet *resultSet = [myAppDelegate.db executeQuery:@"select *from custom1013"];
                
                while ([resultSet next]) {
                    //ID
                    ID = [resultSet stringForColumn:@"id"];
                }
                NSLog(@"當前組的id 是%@",ID);

            }else{
                ID =  self.selectedId;
                NSLog(@"當前組的id是%@",ID);
            }
            
            for(int i = 0; i<self.selected.count;i++){

                NSString *flag = self.selected[i];
                
                //1.選中的話,加入該分組
                if ([flag isEqualToString:@"Y"]) {
                    NSString *myID = self.IDArray[i];
                    count++;
                    NSDate *date_one = [NSDate date];
                    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
                    NSString *updatedTime = [formatter stringFromDate:date_one ];
                    
                    NSString *existsSql = [NSString stringWithFormat:@"update  custom001 set reserve38 = '%@',updated_at = '%@' where id = '%@'", ID ,updatedTime, myID];

                    BOOL ok =  [myAppDelegate.db executeUpdate:existsSql];
                    if (ok) {
                        NSLog(@"加入該分組成功");
                        
                    }else{
                        NSLog(@"加入該分組失敗");
                    }

                  }
                //2.如果未選中的話,將對應欄位設為空
                if([flag isEqualToString:@"N"]){
                    if (!self.selectedId ) {
                        NSString *myID = self.IDArray[i];
                        NSDate *date_one = [NSDate date];
                        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
                        NSString *updatedTime = [formatter stringFromDate:date_one ];
                        
                        NSString *existsSql = [NSString stringWithFormat:@"update  custom001 set reserve38 = '%@',updated_at = '%@' where (id = '%@' and reserve38 = '%@' )", NULL ,updatedTime, myID,self.selectedId];
                        
                        BOOL ok =  [myAppDelegate.db executeUpdate:existsSql];
                        if (ok) {
                            NSLog(@"未選中,加入該分組成功");
                            
                        }else{
                            NSLog(@"未選中,加入該分組失敗");
                        }
                        

                    }
                }
            }
            
            //這邊還需要將數量寫入到對應的資料表中
            NSDate *date_one = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            NSString *updatedTime = [formatter stringFromDate:date_one ];
            
            
            //id 主鍵自增長
            //sys_id
            //status_id
            //custom_id
            //modifydate
            //reserve1 分組名
            //reserve2 成員數量 --暫時為0
            //remark  @"remark"
            //update_sales
            //created_at 建立時間
            //updated_at 更新時間
            //seller_id
            NSString *countStr = [NSString stringWithFormat:@"%d",count];
            NSString *existsSql = [NSString stringWithFormat:@"update  custom1013 set reserve2 = '%@',updated_at = '%@' where id = '%@'",countStr,updatedTime,ID];
            
            NSLog(@"sql is %@",existsSql);
            BOOL ok =  [myAppDelegate.db executeUpdate:existsSql];
            if (ok) {
                NSLog(@"分組數量加入成功");
                
            }else{
                NSLog(@"分組數量加入失敗");
            }
            
            NSLog(@"開始跳轉到前面");
           [self.navigationController popViewControllerAnimated:YES ];

        }else{
            NSLog(@"新增通訊錄人到分組時  資料庫開啟失敗");
        }
        
    }
    NSLog(@"btn的title是 %@",btn.titleLabel.text);
}


自定義的view:addTableviewFootView的標頭檔案和.m檔案
#import <UIKit/UIKit.h>


@protocol myTabAddVdelegate <NSObject>

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

@interface addTableViewFootView : UIView


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

@property(assign,nonatomic)id<myTabAddVdelegate>delegate;




@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];
}