一、自定义视图
新建一个类继承自UIView
@protocol RefreshViewDelegate <NSObject> //写一个协议
@required
- (void)refreshData;
@end
@interface RefreshView : UIView
- (IBAction)buttonAction:(id)sender;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *myActivity;
@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (assign ,nonatomic) id<RefreshViewDelegate> delegate; //代理
- (void)startLoading;
- (void)stopLoading;
- (IBAction)buttonAction:(id)sender {
if([self.delegate respondsToSelector:@selector(refreshData)])
{
[self.delegate refreshData];
}
}
- (void)startLoading
{
[self.myButton setTitle:@"" forState:UIControlStateNormal];
[self.myActivity setHidden:false];
[self.myActivity startAnimating];
}
- (void)stopLoading
{
[self.myButton setTitle:@"加载新数据。。。" forState:UIControlStateNormal];
[self.myActivity setHidden:true];
[self.myActivity stopAnimating];
然后就是要在将要使用这个视图的类里实现自定义视图的协议
@interface YcwFirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,RefreshViewDelegate> //实现了协议
{
NSMutableArray *arr; //数据源
}
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) RefreshView *refreshView;
- (void)loadData;
- (void)setupView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupView];
[self loadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIden = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
static int i= 1;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden];
i++;
}
cell.textLabel.text = [arr objectAtIndex:indexPath.row];
return cell;
}
- (void)setupView
{
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"RefreshView" owner:self options:nil]; //初始化将要是要的自定义视图
self.refreshView = nib[0];
self.refreshView.delegate = self;
self.myTableView.tableFooterView = self.refreshView; //将tableView.tableFooterView 设为自定义视图
[self.view addSubview:self.myTableView];
}
- (void)loadData
{
[self.refreshView startLoading]; //调用自定义视图的startLoading方法
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(startLoadData) userInfo:nil repeats:NO]; //延时3s调用startLoadDate方法
}
- (void)startLoadData
{
if(i == 0)
{
arr = [NSMutableArray arrayWithObjects:@"iphone",@"ipad",@"ipod",@"itouch", nil];
}
else{
[arr addObjectsFromArray:[NSArray arrayWithObjects:@"iphone",@"ipad",@"ipod",@"itouch", nil]];
}
[self.myTableView reloadData]; //
[self.refreshView stopLoading];
}
- (void)refreshData
{
[self loadData];
i++;
}
//refreshData这个方法重要,它是属于自定义视图的协议里面的方法,当视图第一次在界面界面加载完成后,你继续点击试图时,会继续加载新的内容,如果refreshData方法里什么都不实现,当你继续点击自定义视图时,就什么事都不做