iOS中上拉加载下拉刷新之MJRefresh

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     });
相关文章
相关标签/搜索