MJRefresh使用

  1. 使用weak,防止循环引用
 __weak __typeof(self) weakSelf = self;
    
    // 设置回调(一旦进入刷新状态就会调用这个refreshingBlock)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];

2.隐藏状态和时间|自定义状态文字(MJRefreshStateHeader)

    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 隐藏状态
    header.stateLabel.hidden = YES;

    // 设置文字
    [header setTitle:@"Pull down to refresh" forState:MJRefreshStateIdle];
    [header setTitle:@"Release to refresh" forState:MJRefreshStatePulling];
    [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
    
    // 设置字体
    header.stateLabel.font = [UIFont systemFontOfSize:15];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];

    // 设置颜色
    header.stateLabel.textColor = [UIColor redColor];
    header.lastUpdatedTimeLabel.textColor = [UIColor blueColor];

3.加载完毕,自定义加载完毕文字
footer的状态设置为endRefreshingWithNoMoreData

  [footer setTitle:@"No more data" forState:MJRefreshStateNoMoreData];

4.自定义图片动画
继承MJRefreshGifHeader|MJRefreshAutoGifFooter
重写方法- (void)prepare,设置不同状态下的刷新图片

- (void)prepare
{
    [super prepare];
    
    // 设置普通状态的动画图片
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=60; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd", i]];
        [idleImages addObject:image];
    }
     [self setImages:idleImages forState:MJRefreshStateIdle];
    
    // 设置即将刷新状态的动画图片(一松开就会刷新的状态)
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd", i]];
        [refreshingImages addObject:image];
    }
    [self setImages:refreshingImages forState:MJRefreshStatePulling];
    
    // 设置正在刷新状态的动画图片
    [self setImages:refreshingImages forState:MJRefreshStateRefreshing];
}

5.自动上拉刷新
使用MJRefreshAutoFooter,automaticallyRefresh属性设置为YES(默认),triggerAutomaticallyRefreshPercent属性设置为负数,未滑动到底部就自动刷新

6.自定义刷新,加载UI
继承MJRefreshHeader| MJRefreshBackFooter(添加自自动回弹)(MJRefreshFooter)

  • (void)prepare添加UI
  • (void)placeSubviews设置子控件的位置和尺寸
    ….

你可能感兴趣的:(MJRefresh使用)