导入MJRefresh
ViewController.m
#import "ViewController.h"
#import "MJRefresh/MJRefresh.h"
@interface ViewController ()
@property(nonatomic,strong)UITableView *table;
@property(nonatomic,strong)NSMutableArray *arr;
@end
@implementation ViewController
-(UITableView *)table{
if (!_table) {
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStylePlain];
_table.dataSource = self;
_table.delegate = self;
}
return _table;
}
-(NSMutableArray *)arr{
if (!_arr) {
_arr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
}
return _arr;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.table];
//下拉刷新
MJRefreshHeaderView *header = [[MJRefreshHeaderView alloc]init];
header.scrollView = self.table;
header.delegate = self;
//上拉刷新
MJRefreshFooterView *foot = [[MJRefreshFooterView alloc]init];
foot.scrollView = self.table;
foot.delegate = self;
}
//刷新的代理方法
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{
//判断refreshView是上拉的对象 还是下拉的对象
if([refreshView isKindOfClass:[MJRefreshHeaderView class]]){
//延迟3秒后执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//如果是下拉的话
[self.arr addObject:@"other"];
[self.table reloadData];
//下拉刷新停止刷新
[refreshView endRefreshing];
});
}else if([refreshView isKindOfClass:[MJRefreshFooterView class]]){
//延迟3秒后调用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//删除最后一个数据
[self.arr removeLastObject];
//刷新
[self.table reloadData];
//上拉刷新停止刷新
[refreshView endRefreshing];
});
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = [self.arr objectAtIndex:indexPath.row];
return cell;
}
网友评论