@IOS开发中,使用下拉刷新更新UITableView中的数据(最典型的就是新浪微博app,使用下拉的形式来更新最新的微博信息。);上拉加载在UITableView中显示更多的信息(最典型的就是百度贴吧app,在下拉显示一定内容后,通过一个按钮实现加载更多数据)
@上一篇文章介绍了最近MJ大神封装的类,这里讲一个很悠久的上拉下拉类,EGOTableViewPullRefresh,不过它原先也只有下拉刷新功能,最近在网上看到也有了上拉加载功能
@下载地址:https://github.com/emreberge/EGOTableViewPullRefresh
@我用的还是以前的EGOTableView,只有下拉刷新功能,上拉加载请参考另一个博文(推荐用MJ那个第三方,简单)
@代码样例:
#import <UIKit/UIKit.h>
#import "EGORefreshTableHeaderView.h"
@interface NewsListViewController : UITableViewController <EGORefreshTableHeaderDelegate>{
EGORefreshTableHeaderView * _refreshHeaderView;
BOOL _reloading;
NSUInteger number;
}
@property (nonatomic,retain)NSMutableArray * DataArray;
@property (nonatomic,retain)NSMutableArray * oldDataArray;
@property (nonatomic,copy)NSString * dataString;
// 开始重新加载
- (void)reloadTableViewDataSource;
// 完成加载
- (void)doneLoadingTableViewData;
@end
#import "NewsListViewController.h"
#import "EGORefreshTableHeaderView.h"
@interface NewsListViewController ()
@end
@implementation NewsListViewController
- (void)dealloc
{
_refreshHeaderView = nil;
RELEASE_SAFELY(_dataString);
RELEASE_SAFELY(_DataArray);
RELEASE_SAFELY(_oldDataArray);
[super dealloc];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
number = 1;
if (_refreshHeaderView == nil) {
// 初始化下拉刷新控件
EGORefreshTableHeaderView * refreshView = [[EGORefreshTableHeaderView alloc]initWithFrame:CGRectMake(0, -ScreenHeight, ScreenWidth, ScreenHeight)];
refreshView.delegate = self;
// 将下拉刷新控件作为子控件添加到UITableView中
[self.tableView addSubview:refreshView];
[refreshView release];
_refreshHeaderView = refreshView;
}
self.DataArray = [NSMutableArray array];
self.oldDataArray = [NSMutableArray array];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Data Source Loading / Reloading Methods
// 开始重新加载时调用的方法
- (void)reloadTableViewDataSource{
// 将监控刷新状态的变量设置为YES
_reloading = YES;
/*
* ........加载数据的代码
*
*/
// 完成加载
[self doneLoadingTableViewData];
}
#pragma mark - 完成加载时执行的方法
- (void)doneLoadingTableViewData{
// 是否在刷新置为NO
_reloading = NO;
// 结束刷新,界面下拉后就要反弹回去,改动contentInset实现效果
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
// 刷新表格内容
[self.tableView reloadData];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
#pragma mark 下拉被触发调用的委托方法
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
// 一下拉,就调用加载数据
[self reloadTableViewDataSource];
}
#pragma mark 返回当前是刷新还是无刷新状态
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
return _reloading; // should return if data source model is reloading
}
#pragma mark 返回刷新时间的回调方法
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
return [NSDate date]; // should return date data source was last changed
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
@end