1.導入到本身的工程中,若是是非ACR,則須要-fno-objc-arc,若是是arc就不用管了。數組
通常的刷新都是基於UIScrollView的,由於能拖拽的view通常都是繼承於UIScrollView。xcode
2.#import 「MJRefresh.h」 這個能夠直接加載.m中緩存
而後.h文件中:網絡
1 #import <UIKit/UIKit.h>
2
3 @interface MyViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
4 { 5 CGRect _rect;//保存當前物理屏幕大小
6 } 7 @property (nonatomic,retain)UITableView *tableView;//當前的tableview
8 @property (nonatomic,retain)NSMutableArray *tableViewData;//tableview中數組數組
9 @end
.m文件中:ui
1 #import "MyViewController.h"
2
3 #import "MJRefresh.h"
4
5 @interface MyViewController () 6
7 @end
8
9 @implementation MyViewController 10 -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 11 { 12 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 13 if(self){ 14 _rect = [[UIScreen mainScreen]bounds]; 15 _tableViewData = [[NSMutableArray alloc]init]; 16 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, _rect.size.width, _rect.size.height)]; 17 } 18 return self; 19 } 20 - (void)viewDidLoad { 21 [super viewDidLoad]; 22 for (int i = 0 ; i < 5; i++) { 23 [_tableViewData addObject:[NSString stringWithFormat:@"這是第%d條",i + 1]]; 24 } 25 _tableView.delegate = self; 26 _tableView.dataSource = self; 27 [self.view addSubview:_tableView]; 28 //開啓刷新狀態
29 [self setupRefresh]; 30 } 31 // 32 開始刷新自定義方法 33 - (void)setupRefresh 34 { 35 //下拉刷新
36 [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"]; 37 [self.tableView headerBeginRefreshing]; 38
39 // 2.上拉加載更多(進入刷新狀態就會調用self的footerRereshing)
40 [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)]; 41 //一些設置 42 // 設置文字(也能夠不設置,默認的文字在MJRefreshConst中修改)
43 self.tableView.headerPullToRefreshText = @"下拉能夠刷新了"; 44 self.tableView.headerReleaseToRefreshText = @"鬆開立刻刷新了"; 45 self.tableView.headerRefreshingText = @"刷新中。。。"; 46
47 self.tableView.footerPullToRefreshText = @"上拉能夠加載更多數據了"; 48 self.tableView.footerReleaseToRefreshText = @"鬆開立刻加載更多數據了"; 49 self.tableView.footerRefreshingText = @"加載中。。。"; 50 } 51 //下拉刷新
52 - (void)headerRereshing 53 { 54 //通常這些個裏邊是網絡請求,而後會有延遲,不會像如今刷新這麼快 55 // 1.添加假數據
56 [self.tableViewData insertObject:@"這是刷新的數據" atIndex:0]; 57 [self.tableView reloadData]; 58 //2.結束刷新
59 [self.tableView headerEndRefreshing]; 60 } 61 //上拉加載
62 - (void)footerRereshing 63 { 64 //這個通常是提取緩存的數據 65 // 1.添加假數據
66 [self.tableViewData insertObject:@"這是加載之前的數據" atIndex:[_tableViewData count]]; 67 [self.tableView reloadData]; 68 //2,結束刷新
69 [self.tableView footerEndRefreshing]; 70 } 71 // 72
73 #pragma mark - tableView delegate
74
75 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 76 { 77 return 1; 78 } 79 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 80 { 81 return [_tableViewData count]; 82 } 83 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 84 { 85 static NSString *cellID = @"cellID"; 86 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 87 if(cell == nil){ 88 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; 89 } 90 cell.textLabel.text = _tableViewData[indexPath.row]; 91 return cell; 92 } 93 //釋放
94 - (void)dealloc 95 { 96 [_tableView release]; 97 [_tableViewData release]; 98 [super dealloc]; 99 } 100
101 若是在xcode6.0中有錯誤: 102 選中項目 - Project - Build Settings-Apple LLVM 6.0-Preprocessing中的Enable Strict Checking of objc_msgsend calls 設置爲 NO 便可
PS:atom
1 //時間隊列,規定時間執行某個事件
2 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 3 // 刷新表格
4 [self.tableView reloadData]; 5
6 // (最好在刷新表格後調用)調用endRefreshing能夠結束刷新狀態
7 [self.tableView headerEndRefreshing]; 8 });