#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 = ;
NSMutableArray *array = [NSMutableArray array];
//每次刷新五条数据
for (int i = ; i < ; 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 = ;
NSMutableArray *array = [NSMutableArray array];
for (int j = ; j < ; 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
05-08 08:19