////---------------自定义TableFootViewDelegate类,用来管理对应的Xib文件----------------
#import <UIKit/UIKit.h>
//该协议用来规范了加载更多数据的方法(需要用到控制器中的加载更多的方法)
@protocol MLTableFooterViewDelegate <NSObject>
@optional
-(void)loadingMoreData;
@end
@interface MLTableFooterView : UIView
@property(nonatomic , weak)id<MLTableFooterViewDelegate> delegate;
//用来快速创建一个footView对象.
+(instancetype)footerView;
-(instancetype)initFooterView;
@end
#import "MLTableFooterView.h"
@interface MLTableFooterView ()
@property (weak, nonatomic) IBOutlet UIButton *loadBtn;
@property (weak, nonatomic) IBOutlet UIView *loadingView;
-(IBAction)loadBtnClick;
@end
@implementation MLTableFooterView
+(instancetype)footerView{
return [[self alloc]initFooterView];
}
-(instancetype)initFooterView{
//初始化一个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:@"MLTableFooterView" 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(loadingMoreData)]) {
//加载数据
[self.delegate loadingMoreData];
//显示加载按钮
self.loadBtn.hidden = NO;
//隐藏"正在加载"
self.loadingView.hidden = YES;
}
});
}
@end
////------控制器中部分代码------------(需要实现协议MLTableFooterViewDelegate)
//使用自定义的tableFooterView
MLTableFooterView *footer = [MLTableFooterView footerView];
footer.delegate = self;
//将tableView的代理对象设置为当前类对象.
self.tableView.tableFooterView = footer;
//实现协议的loadingMoreData方法,加载更多数据
-(void)loadingMoreData{
//添加更多的模型数据(虚拟数据)
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];
}原文地址:http://my.oschina.net/u/2285956/blog/355137