iOS中MJRefresh框架的浅析

支持的控件:UITableView,UICollectionView,UIScrollView,UIWebView
//防止循环引用
__weak UITableView *tableView = self.tableView;
__weak typeof(self)weakSelf = self;

 MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHouseData)];
 MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [weakSelf refreshHouseData];
   }];
tableView.mj_header = header;
//自定义文字
[header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
[header setTitle:@"松开就可以刷新的状态" forState:MJRefreshStatePulling];
[header setTitle:@"正在努力刷新中...." forState:MJRefreshStateRefreshing];
[header setTitle:@"刷新完毕,没有更多数据" forState:MJRefreshStateNoMoreData];

header.lastUpdatedTimeLabel.hidden = NO;//隐藏上次更新时间
header.stateLabel.hidden = NO;// 隐藏状态
//设置字体
header.stateLabel.font = [UIFont systemFontOfSize:15];
header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:15];
//设置颜色
header.stateLabel.textColor = [UIColor redColor];
header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];


MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreHouseData)];
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreHouseData];
    }];
tableView.mj_footer = footer;
[footer setTitle:@"上拉加载" forState:MJRefreshStateIdle];
[footer setTitle:@"松开就可以加载的状态" forState:MJRefreshStatePulling];
[footer setTitle:@"正在努力加载中...." forState:MJRefreshStateRefreshing];
[footer setTitle:@"加载完毕,没有更多数据" forState:MJRefreshStateNoMoreData];
设置简单的动画
tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf refreshHouseData];
    }];
[header setImages:imageArr1  forState:MJRefreshStateIdle];
[header setImages:imageArr2  forState:MJRefreshStateRefreshing];
简单案例
#import "ViewController.h"
#import "MJRefresh.h"
@interface ViewController ()
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加一个tableview
    self.tableView= [[UITableView alloc]initWithFrame:self.view.bounds];

    self.tableView.delegate=self;

    self.tableView.dataSource=self;

    [self.view addSubview:_tableView];


    //创建下拉刷新
    MJRefreshNormalHeader* header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [self performSelector:@selector(headRefresh)withObject:nil afterDelay:2.0f];

    }];

    //设置自定义文字,因为默认是英文的
    [header setTitle:@"下拉刷新"forState:MJRefreshStateIdle];

    [header setTitle:@"松开加载更多"forState:MJRefreshStatePulling];

    [header setTitle:@"正在刷新中"forState:MJRefreshStateRefreshing];


    self.tableView.mj_header= header;

    //创建上拉刷新
    MJRefreshBackNormalFooter * foot =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{

        [self performSelector:@selector(footRefresh)withObject:nil afterDelay:2.0f];

    }];
    self.tableView.mj_footer= foot;

    [foot setTitle:@"上拉刷新"forState:MJRefreshStateIdle];

    [foot setTitle:@"松开加载更多"forState:MJRefreshStatePulling];

    [foot setTitle:@"正在刷新中"forState:MJRefreshStateRefreshing];

}
- (void)headRefresh {
    NSLog(@"下拉,加载数据");
    [self.tableView.mj_header endRefreshing];
}
- (void)footRefresh {
    NSLog(@"上拉,加载数据");
    [self.tableView.mj_footer endRefreshing];
}



# pragma mark - tabelView代理方法.

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

    return 10;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

    NSString* cellReuseIdentifier =@"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellReuseIdentifier];

    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];

    }

    cell.textLabel.text=@"hello,MjRefresh";

    return cell;

}


@end

你可能感兴趣的:(iOS中MJRefresh框架的浅析)