上拉刷新下拉加载比较流行的两个第三方 MJRefresh和 EGOTableViewPullRefresh
一.最新版的MJRefresh
首先介绍M了个J的 最新版的MJRefresh
因为他的github里有详细介绍,话不多说上代码
- //
- // ViewController.m
- // 新版MJRefresh Demo
- //
- // Created by Jack_Jia on 16/1/19.
- // Copyright © 2016年 Jack_Jia. All rights reserved.
- //
- #import "ViewController.h"
- #import "MJRefresh/MJRefresh.h"
- @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
- {
- UITableView *_tableView;
- NSMutableArray *_arr;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor purpleColor];
- _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- [self.view addSubview:_tableView];
- _arr = [NSMutableArray arrayWithObjects:@"111",@"222",@"333",@"444",@"555", nil nil];
- //添加下拉加载
- //_tableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(hander)];
- //_tableView.mj_header.lastUpdatedTimeKey key用来存储上一次下拉刷新成功的时间
- //_tableView.mj_header.lastUpdatedTime 上一次下拉刷新成功的时间
- // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
- MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(hander)];
- // 设置普通状态的动画图片
- [header setImages:(NSArray *)[UIImage imageNamed:@"arrow"] forState:MJRefreshStateIdle];
- // 设置即将刷新状态的动画图片(一松开就会刷新的状态)
- [header setImages:(NSArray *)[UIImage imageNamed:@"arrow"]forState:MJRefreshStatePulling];
- // 设置正在刷新状态的动画图片
- [header setImages:(NSArray *)[UIImage imageNamed:@"arrow"] forState:MJRefreshStateRefreshing];
- // 隐藏时间
- //header.lastUpdatedTimeLabel.hidden = YES;
- // 隐藏状态
- //header.stateLabel.hidden = YES;
- // 设置文字
- [header setTitle:@"Pull down to refresh" forState:MJRefreshStateIdle];
- [header setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
- [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
- // 设置字体
- header.stateLabel.font = [UIFont systemFontOfSize:15];
- header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
- // 设置颜色
- header.stateLabel.textColor = [UIColor redColor];
- header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];
- // 设置header
- _tableView.mj_header = header;
- //添加上拉刷新 (同理)
- _tableView.mj_footer = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)hander
- {
- //多线程 延时调用
- [self performSelector:@selector(addMore) withObject:nil afterDelay:3];
- }
- - (void)addMore
- {
- [_arr addObject:@"666"];
- [_tableView reloadData];
- //结束刷新
- [_tableView.mj_header endRefreshing];
- /* 刷新状态控制
- 进入刷新状态
- - (void)beginRefreshing;
- 结束刷新状态
- - (void)endRefreshing;
- 是否正在刷新
- - (BOOL)isRefreshing;
- */
- }
- - (void)footer
- {
- [self performSelector:@selector(refresh) withObject:nil];
- }
- -(void)refresh
- {
- [_arr addObject:@"777"];
- [_tableView reloadData];
- //结束刷新
- [_tableView.mj_header endRefreshing];
- }
- #pragma mark UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _arr.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellId = @"cellId";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
- if (!cell) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- }
- cell.textLabel.text = _arr[indexPath.row];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [_arr removeObject:_arr[indexPath.row]];
- [_tableView reloadData];
- }
- @end
二.EGOTableViewPullRefresh
- <span style="font-size:18px;">#import "ViewController.h"
- #import "PullTableView.h"
- @interface ViewController ()<PullTableViewDelegate,UITableViewDataSource,UITableViewDelegate>
- {
- PullTableView *_tableView;
- NSMutableArray *_arr;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor magentaColor];
- _tableView = [[PullTableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.pullDelegate = self;
- _arr = [NSMutableArray arrayWithObjects:@"111",@"222",@"333",@"444",@"555", nil nil];
- //背景颜色
- _tableView.pullBackgroundColor = [UIColor lightGrayColor];
- //文字颜色
- _tableView.pullTextColor = [UIColor purpleColor];
- //改变加载刷新图片
- _tableView.pullArrowImage = [UIImage imageNamed:@"tabBar_Found"];
- //_tableView.pullLastRefreshDate = myDate;//多长时间前更新过 计时
- //_tableView.pullTableIsRefreshing = YES; //页面显示就刷新
- //_tableView.pullTableIsLoadingMore = YES;//页面显示就加载
- //文字可通过搜索功能改成你要的文字
- [self.view addSubview:_tableView];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _arr.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellId = @"cellId";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
- if (!cell) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- }
- cell.textLabel.text = _arr[indexPath.row];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [_arr removeObject:_arr[indexPath.row]];
- [_tableView reloadData];
- }
- #pragma mark PullTableViewDelegate
- -(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
- {
- NSLog(@"上拉加载");
- //多线程 延时调用
- [self performSelector:@selector(addMore) withObject:nil afterDelay:3];
- }
- -(void)addMore
- {
- [_arr addObject:@"777"];
- [_tableView reloadData];
- //关闭加载
- _tableView.pullTableIsLoadingMore=NO;
- }
- -(void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
- {
- NSLog(@"下拉刷新");
- //多线程 延时调用
- [self performSelector:@selector(refresh) withObject:nil afterDelay:3];
- }
- -(void)refresh
- {
- [_arr addObject:@"666"];
- [_tableView reloadData];
- //关闭刷新
- _tableView.pullTableIsRefreshing=NO;
- }
- @end</span>
EGOTableViewPullRefresh github : https://github.com/enormego/EGOTableViewPullRefresh
MJRefresh github: https://github.com/CoderMJLee/MJRefresh
本文综合版Demo : http://download.csdn.net/detail/jackjia2015/9410080
来源:https://www.cnblogs.com/ios4app/p/6855960.html