使用MJRefresh实现上拉加载,下拉刷新功能!!
2015-08-04 10:42
441 查看
注:本文转自于:http://blog.csdn.net/mitianbingshi/article/details/17076273
#import "HomeViewController.h"
#import "MJRefresh.h"
@interface HomeViewController ()
{
MJRefreshHeaderView *_header;
MJRefreshFooterView *_footer;
}
@property (nonatomic ,retain) UITableView *mainTableView;
@property (nonatomic ,retain) NSMutableArray *mainDataList;
@end
@implementation HomeViewController
@synthesize mainTableView,mainDataList;
- (void)viewDidLoad
{
[super viewDidLoad];
mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
mainTableView.delegate = self;
mainTableView.dataSource = self;
[self.view addSubview:mainTableView];
mainDataList = [[NSMutableArray alloc]init];
// 刷新功能
_header = [[MJRefreshHeaderView alloc] init];
_header.delegate = self;
_header.scrollView = mainTableView;
//添加上拉加载更多
_footer = [[MJRefreshFooterView alloc] init];
_footer.delegate = self;
_footer.scrollView = mainTableView;
}
#pragma mark 代理方法-进入刷新状态就会调用
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"HH : mm : ss.SSS";
if (_header == refreshView)
{
//下拉刷新数据请求********************************
// 2秒后刷新表格
[self performSelector:@selector(reloadDeals) withObject:nil afterDelay:2];
} else {
//上拉刷新数据请求**************************************
// 2秒后刷新表格
[self performSelector:@selector(reloadDeals) withObject:nil afterDelay:2];
}
}
- (void)reloadDeals
{
[mainTableView reloadData];
// 结束刷新状态
[_header endRefreshing];
[_footer endRefreshing];
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mainDataList.count;
}
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellindentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier];
}
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "HomeViewController.h"
#import "MJRefresh.h"
@interface HomeViewController ()
{
MJRefreshHeaderView *_header;
MJRefreshFooterView *_footer;
}
@property (nonatomic ,retain) UITableView *mainTableView;
@property (nonatomic ,retain) NSMutableArray *mainDataList;
@end
@implementation HomeViewController
@synthesize mainTableView,mainDataList;
- (void)viewDidLoad
{
[super viewDidLoad];
mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
mainTableView.delegate = self;
mainTableView.dataSource = self;
[self.view addSubview:mainTableView];
mainDataList = [[NSMutableArray alloc]init];
// 刷新功能
_header = [[MJRefreshHeaderView alloc] init];
_header.delegate = self;
_header.scrollView = mainTableView;
//添加上拉加载更多
_footer = [[MJRefreshFooterView alloc] init];
_footer.delegate = self;
_footer.scrollView = mainTableView;
}
#pragma mark 代理方法-进入刷新状态就会调用
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"HH : mm : ss.SSS";
if (_header == refreshView)
{
//下拉刷新数据请求********************************
// 2秒后刷新表格
[self performSelector:@selector(reloadDeals) withObject:nil afterDelay:2];
} else {
//上拉刷新数据请求**************************************
// 2秒后刷新表格
[self performSelector:@selector(reloadDeals) withObject:nil afterDelay:2];
}
}
- (void)reloadDeals
{
[mainTableView reloadData];
// 结束刷新状态
[_header endRefreshing];
[_footer endRefreshing];
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mainDataList.count;
}
-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellindentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindentifier];
}
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- 初始化端点错误--Error initializing endpoint
- JS 日期时间比较 值类型和引用类型
- Android Studio 1.3 发布
- The Zen of Python
- String、StringBuffer和StringBuilder
- 反射与IOC
- android webview type=file文件上传,安卓端代码
- [hdu1085]生成函数
- POJ 3264 线段树模板题
- 修改mysql用户名密码 和 PHPmysqlAdmin对应密码修改
- struts1
- 不能创建src/main/java 源文件夹的问题
- 短信返回验证码功能的实现
- 使用Redmine进行项目管理
- Linux设备驱动核心理论(一)
- Little Keng
- 实例代码:AcDbDatabase符号表的当前值
- TextView预渲染研究
- 代码生成器:Java自动生成service,serviceImpl及action
- 关于文件的操作