上拉下拉刷新

導(dǎo)入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是上拉的對(duì)象 還是下拉的對(duì)象

if([refreshView isKindOfClass:[MJRefreshHeaderView class]]){

//延遲3秒后執(zhí)行

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秒后調(diào)用

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

//刪除最后一個(gè)數(shù)據(jù)

[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;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容