您的位置:首页 > 其它

下拉刷新和上拉加载更多(第三方框架MJRefresh)

2015-07-06 20:01 387 查看
#import "RootViewController.h"
#import "MJRefresh.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate>
{
UITableView *_tableView ;
NSMutableArray *datas;
MJRefreshHeaderView *headerView;
MJRefreshFooterView *footerView;
}
@end

@implementation RootViewController

- (void)dealloc
{
[headerView free];
[footerView free];
}

- (void)viewDidLoad {
[super viewDidLoad];
//初始化tableView
[self initializeTableView];
// 初始化数据
[self initializeData];
}

- (void)initializeTableView
{
_tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// 初始化刷新控件
[self initializeRefreshView];
}

- (void)initializeRefreshView
{
// 下拉刷新
headerView = [MJRefreshHeaderView header];
headerView.scrollView = _tableView;
headerView.delegate = self;

// 上拉加载更多
footerView = [MJRefreshFooterView footer];
footerView.delegate = self;
footerView.scrollView = _tableView;
}

/**
*  刷新控件进入开始刷新状态的时候调用
*/
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) {
//下拉刷新
[self loadNewData];
}else{
[self loadMoreData];
}
}
/*
* 刷新数据
*/
- (void)loadNewData
{
static int count = 1;
NSMutableArray *array = [NSMutableArray array];
//每次刷新五条数据
for (int i = 0; i < 5; i++) {
NSString *str = [NSString stringWithFormat:@"第%d次刷新",count];
[array addObject:str];
}
// 把新数据加到datas前面
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:array];
[newArray addObjectsFromArray:datas];
datas = newArray;
[_tableView reloadData];
[headerView endRefreshing];
count++;
}

/*
* 加载更多数据
*/
- (void)loadMoreData
{
static int count = 1;
NSMutableArray *array = [NSMutableArray array];
for (int j = 0; j < 5; j++) {
NSString *str = [NSString stringWithFormat:@"第%d次加载更多",count];
[array addObject:str];
}
[datas addObjectsFromArray:array];
[_tableView reloadData];
[footerView endRefreshing];
count++;
}

- (void)initializeData
{
datas = [NSMutableArray arrayWithObjects:@"A",@"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z",  nil];
}

#pragma mark -配置UITableViewDataSource数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identify = @"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];

}
cell.textLabel.text = datas[indexPath.row];
return cell;
}

@end



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: