IOS自定義TableFooterView

  1. 自定義類MLTgFooterView 和 MLTgFooterViewDelegate數組

#import <UIKit/UIKit.h>
/**
 1.協議名稱: 控件類名 + Delegate
 2.代理方法廣泛都是@optional
 3.代理方法通常以空間名開頭,不包含前綴
 */

@protocol MLTgFooterViewDelegate <NSObject>

@optional//不強求實現
-(void)tgFooterViewDidClickedLoadBtn:(MLTgFooterView *)tgFooterView;
@end

@interface MLTgFooterView : UIView

@property(nonatomic , weak)id<MLTgFooterViewDelegate> delegate;
//用來快速建立一個footView對象.
+(instancetype)fgFooterView;
-(instancetype)initTgFooterView;

@end


 2.自定義類MLTgFooterView的實現atom

#import "MLTgFooterView.h"

@interface MLTgFooterView ()
@property (weak, nonatomic) IBOutlet UIButton *loadBtn;
@property (weak, nonatomic) IBOutlet UIView *loadingView;

-(IBAction)loadBtnClick;

@end

@implementation MLTgFooterView

+(instancetype)fgFooterView{
    return [[self alloc]initTgFooterView];
}
-(instancetype)initTgFooterView{
    
    //初始化一個nib對象(包含xib中的全部信息)-----另外一種加載Xib文件的方法
    //UINib *nib = [UINib nibWithNibName:@"MLTableFooterView" bundle:nil];
    
    //返回的是xib中全部的文件的數組,由於此xib中只有一個,故用fistObject獲取改自定義的View.
    //UIView *footView =  [[nib instantiateWithOwner:nil options:nil] firstObject];

    
    return [[[NSBundle mainBundle] loadNibNamed:@"MLTgFooterView" owner:nil options:nil] firstObject];
}

-(IBAction)loadBtnClick{
    //隱藏加載按鈕
    self.loadBtn.hidden = YES;
    //顯示正在加載的view
    self.loadingView.hidden = NO;
    
    //顯示更多數據
    //使用C語言實現延遲
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if ([self.delegate respondsToSelector:@selector(tgFooterViewDidClickedLoadBtn:)]) {
            //加載數據
            [self.delegate tgFooterViewDidClickedLoadBtn:self];
            //顯示加載按鈕
            self.loadBtn.hidden = NO;
            //隱藏"正在加載"
            self.loadingView.hidden = YES;
            
        }
    });
    
}


  3.控制器實現自定義tableFooterView的部分代碼,以及使用實現MLTgFooterViewDelegate協議來完成數據的模擬加載.(首先控制器要繼承MLTgFooterViewDelegate協議)spa

//使用自定義的tableFooterView
    MLTgFooterView *footer = [MLTgFooterView fgFooterView];
    footer.delegate = self;
    
    self.tableView.tableFooterView = footer;

    

-(void)tgFooterViewDidClickedLoadBtn:(MLTgFooterView *)tgFooterView{
    //添加更多的模型數據(虛擬數據)
    MLTg *tg = [[MLTg alloc]init]; //其中MLTg是個數據模型,_tgs是控制器的成員,用來保存全部的數據模型.
    tg.icon = @"ad_00";
    tg.title = @"新增長的團購數據..";
    tg.price = @"100";
    tg.buyCount = @"0";
    
    //將數據天道_tgs中
    [_tgs addObject:tg];
    //刷新表格(告訴tableView從新加載模型數據,調用tableView的reloadData)
    [self.tableView reloadData];
}
相關文章
相關標籤/搜索