#import "MyViewController.h"
#import "MJRefresh.h"
@interface MyViewController ()
@end
@implementation MyViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
_rect = [[UIScreen mainScreen]bounds];
_tableViewData = [[NSMutableArray alloc]init];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, _rect.size.width, _rect.size.height)];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
for (int i = 0 ; i < 5; i++) {
[_tableViewData addObject:[NSString stringWithFormat:@"这是第%d条",i + 1]];
}
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
//开启刷新状态
[self setupRefresh];
}
//
开始刷新自定义方法
- (void)setupRefresh
{
//下拉刷新
[self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
[self.tableView headerBeginRefreshing];
// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
[self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
//一些设置
// 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
self.tableView.headerRefreshingText = @"刷新中。。。";
self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
self.tableView.footerRefreshingText = @"加载中。。。";
}
//下拉刷新
- (void)headerRereshing
{
//一般这些个里边是网络请求,然后会有延迟,不会像现在刷新这么快
// 1.添加假数据
[self.tableViewData insertObject:@"这是刷新的数据" atIndex:0];
[self.tableView reloadData];
//2.结束刷新
[self.tableView headerEndRefreshing];
}
//上拉加载
- (void)footerRereshing
{
//这个一般是提取缓存的数据
// 1.添加假数据
[self.tableViewData insertObject:@"这是加载以前的数据" atIndex:[_tableViewData count]];
[self.tableView reloadData];
//2,结束刷新
[self.tableView footerEndRefreshing];
}
//
#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tableViewData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.textLabel.text = _tableViewData[indexPath.row];
return cell;
}
//释放
- (void)dealloc
{
[_tableView release];
[_tableViewData release];
[super dealloc];
}