MJRefresh是一款非常好用的上拉下拉第三方庫,使用也很簡單。github地址: https://github.com/CoderMJLee/MJRefresh 。
下拉刷新
官方給過來的例子很簡單,默認使用如下:
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ // 進入刷新狀態后會自動調用這個block }]; 或 // 設置回調(一旦進入刷新狀態,就調用target的action,也就是調用self的loadNewData方法) self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)]; // 馬上進入刷新狀態 [self.tableView.header beginRefreshing];
結束下拉刷新:
// 拿到當前的下拉刷新控件,結束刷新狀態 [self.tableView.header endRefreshing];
上拉刷新
官方給過來的默認例子:
self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ // 進入刷新狀態后會自動調用這個block }]; 或 // 設置回調(一旦進入刷新狀態,就調用target的action,也就是調用self的loadMoreData方法) self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
結束上拉刷新:
// 拿到當前的上拉刷新控件,結束刷新狀態 [self.tableView.footer endRefreshing];
從上面,我們可以看到,一般情況下,進行頁面的時候,我們會使用下拉刷新,並“馬上進入刷新狀態”,網絡請求完數據后,結束“下拉刷新狀態”。但上拉刷新,就不需要“馬上進入刷新狀態”了。
更多使用例子,請參數官方給過來的例子,使用起來還是挺方便的。
下面,給出某個項目的實際使用代碼:
// // NJBillTableViewController.m // NJWisdomCard // // Created by admin on 15/8/21. // Copyright (c) 2015年 Weconex. All rights reserved. // #import "NJBillTableViewController.h" #import "NJBillListTableviewCell.h" #import "Common.h" #import "NJHttpToolHandle.h" #import "MBProgressHUD+NJ.h" #import "NJAccountTool.h" #import "MJExtension.h" #import "MJRefresh.h" #import "NJAccountModel.h" #import "NJBillModel.h" @interface NJBillTableViewController() /** * 賬單模型 */ @property (nonatomic, strong) NSMutableArray *billsFrames; /** * 頁數 */ @property (nonatomic,assign) int pageIndex; @end @implementation NJBillTableViewController - (NSMutableArray *)billsFrames { if (!_billsFrames) { self.billsFrames = [NSMutableArray array]; } return _billsFrames; } - (void)viewDidLoad { [super viewDidLoad]; //集成下拉刷新控件 [self setupDownRefresh]; //集成上拉刷新控件 [self setupUpRefresh]; } /** * 集成上拉刷新控件 */ - (void)setupUpRefresh { self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)]; } /** * 集成下拉刷新控件 */ - (void)setupDownRefresh { // 設置回調(一旦進入刷新狀態,就調用target的action,也就是調用self的loadNewData方法) self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewBills)]; // 馬上進入刷新狀態 [self.tableView.header beginRefreshing]; } /** * 加載下拉刷新數據 */ - (void)loadNewBills { _pageIndex=1;//默認加載第一頁 [self.billsFrames removeAllObjects];//移除所有的數據 //1.從沙盒里拿用戶模型 NJAccountModel *accountModel=[NJAccountTool accountModel]; // 2.拼接請求參數 NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"userId"] =accountModel.loginId;//登錄號 params[@"page"] =[NSString stringWithFormat:@"%d",_pageIndex];//頁數 params[@"pagesize"] =[NSString stringWithFormat:@"10"]; //3.發送請求 [NJHttpToolHandle postWithServiceCode:@"queryAllBalance" params:params success:^(id responseObject) { if ([responseObject[@"resultCode"] isEqualToString:@"0000"]) { //獲取value數組 NSDictionary *dictList = [NSJSONSerialization JSONObjectWithData:[responseObject[@"code"] JSONData] options:NSJSONReadingMutableLeaves error:nil]; // 將 "賬單字典"數組 轉為 "賬單模型"數組 NSArray *newBills = [NJBillModel objectArrayWithKeyValuesArray:dictList[@"value"]]; // 將最新的賬單數據,添加到總數組的最前面 NSRange range = NSMakeRange(0, newBills.count); NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range]; [self.billsFrames insertObjects:newBills atIndexes:set]; //[self.billsFrames insertObjects:newBills atIndex:0]; //[self.billsFrames insertObjects:newBills atIndexes:0]; // 刷新表格 [self.tableView reloadData]; // 拿到當前的下拉刷新控件,結束刷新狀態 [self.tableView.header endRefreshing]; } else { [self.tableView.header endRefreshing]; } } failure:^(NSError *error) { [self.tableView.header endRefreshing]; }]; } /** * 加載上拉刷新數據 */ -(void)loadMoreBills { //1.設置頁數 _pageIndex++;//默認加載第一頁 // 2.拼接請求參數 NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"userId"] =[NJAccountTool accountModel].loginId;//登錄號 params[@"page"] =[NSString stringWithFormat:@"%d",_pageIndex];//頁數 params[@"pagesize"] =[NSString stringWithFormat:@"10"]; //3.發送請求 [NJHttpToolHandle postWithServiceCode:@"queryAllBalance" params:params success:^(id responseObject) { if ([responseObject[@"resultCode"] isEqualToString:@"0000"]) { //獲取value數組 NSDictionary *dictList = [NSJSONSerialization JSONObjectWithData:[responseObject[@"code"] JSONData] options:NSJSONReadingMutableLeaves error:nil]; // 將 "賬單字典"數組 轉為 "賬單模型"數組 NSArray *newBills = [NJBillModel objectArrayWithKeyValuesArray:dictList[@"value"]]; // 將更多的賬單數據,添加到總數組的最后面 [self.billsFrames addObjectsFromArray:newBills]; // 刷新表格 [self.tableView reloadData]; // 拿到當前的下拉刷新控件,結束刷新狀態 [self.tableView.footer endRefreshing]; } else { [self.tableView.footer endRefreshing]; } } failure:^(NSError *error) { [self.tableView.footer endRefreshing]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NJBillListTableviewCell *cell = [NJBillListTableviewCell cellWithTableView:tableView]; cell.billModel=self.billsFrames[indexPath.item]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.billsFrames.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80; } @end