The second package MJRefresh

Refresh with the usual use of open source MJRefresh , refresh the library presumably also many developers are more familiar with a library, a way of using inheritance, different levels have different functions and UI display, using them is simple and convenient.


MJ is a UIScrollView wrote category class, to increase the UIScrollView mj_header(pull-down refresh) and mj_footer(LAC on upload more) two associated objects. MJ example of Li Mingjie teacher used as follows:

  • Pull down to refresh
  __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf refreshAction];//这里可以做请求网络等操作。
    }];
    刷新的操作结束后,调用下边方法,结束刷新
    [self.tableView.mj_header endRefreshing];
复制代码
  • On Raja upload more
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefreshAction];//请求更多数据
    }];
    //可以根据请求的结果来以不同的方式结束刷新。
     [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    
复制代码

These are just examples, MJ author to provide a variety of different UI and interaction of the header and footer for your choosing.

Demo usually so directly in accordance with the example of the can, but personally always felt very good. Able to think of the next package (mostly lazy, I want to save something).

Let me talk about the basic needs in: tableView a list of 20 requests per page (Note: Different companies may be different from the number of each page, here is an example) of data, drop-down refresh time is re-request the data of the first page, the LAC upload more often request the next data. If the next page of data or 20, believes there is more data, it will end in a normal state footr refresh. Raja uploaded on again when the data for the next request will be normal. If the load is less than the number of data out of the data page article (20), then that is no more data, and then it will footer at the end of the state is no more data refresh, and then uploaded when the LAC, It will not work.

I want to effect that external when I called, I just want to know the trigger to request more time to load and refresh, do not want the other tube.

I now realize is: to write a UITableView category (UICollectionView Similarly .UIScrollView can also pull down to refresh, just do not upload more on the LAC). Write two methods, namely the pull-down refresh and calls on the Laga upload. Two associated object while increasing: the number of pieces of data and a block on the page footer set callback after carrying more LAC. Sample code is as follows:

#define WeakSelf __weak typeof(self) weakSelf = self;

typedef void(^FooterConfigBlock)(NSInteger newDataCount);
typedef void(^RefreshActionBlock)(FooterConfigBlock footerConfig);
@interface UITableView (Refresh)
/**
 每页的数据条数
*/
@property (nonatomic,strong)NSNumber *pageCount;

/**
 设置footer的回调
 */
@property (nonatomic,copy)void(^footerConfigBlock)(NSInteger newCount);

/**
 header的刷新

 @param refreshBlock 刷新的回调,回调block里有个"FooterConfigBlock"的参数,外部调用的时候可以将请求下拉的数据的条数传进来,让方法里边对footer进行设置。eg:如果下拉刷新都没有数据的话,就可以直接将不要footer。
 */
- (void)normalHeaderRefreshingActionBlock:(RefreshActionBlock)refreshBlock;

/**
 footer的刷新加载更多

 @param footerRefreshBlock 加载更多的回调。回调block里有个"FooterConfigBlock"的参数,外部调用的时候可以将请求下拉的数据的条数传进来,让方法里边对footer进行设置。eg:如果上拉加载返回的数据小于每页的设置条目数量,则认为数据已经加载完毕,可以将footer设置为没有更多数据的状态。
 */
- (void)backNormalFooterRefreshingActionBlock:(RefreshActionBlock)footerRefreshBlock;

@end


@implementation UITableView (Refresh)
#pragma mark --关联对象----------------
- (void)setPageCount:(NSNumber *)pageCount
{
    objc_setAssociatedObject(self, @selector(setPageCount:), pageCount, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)pageCount
{
    return objc_getAssociatedObject(self, @selector(setPageCount:));
}

- (void)setFooterConfigBlock:(void (^)(NSInteger))footerConfigBlock
{
    objc_setAssociatedObject(self, @selector(setFooterConfigBlock:), footerConfigBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(NSInteger))footerConfigBlock
{
    return objc_getAssociatedObject(self, @selector(setFooterConfigBlock:));
}


#pragma mark --header刷新----------------

- (void)normalHeaderRefreshingActionBlock:(RefreshActionBlock)refreshBlock
{
//如果外部调用的时候不设置每页的数据条目数量pageCount。这里设置一个默认值。
    self.pageCount = [self.pageCount integerValue] > 0 ? self.pageCount : @(20);
    WeakSelf
    //将外部的block给关联对象赋值,如果外部需要内部进行footer和header结束刷新的处理的话,可以传block进来。
    self.footerConfigBlock = ^(NSInteger newCount) {
        [weakSelf handleEndRefreshing:newCount];
    };
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        refreshBlock(weakSelf.footerConfigBlock);
    }];
}
#pragma mark --footer刷新----------------
- (void)backNormalFooterRefreshingActionBlock:(RefreshActionBlock)footerRefreshBlock
{
//同理设置一个默认的每页数据条目
    self.pageCount = [self.pageCount integerValue] > 0 ? self.pageCount : @(20);
    WeakSelf
    self.footerConfigBlock = ^(NSInteger newCount) {
        [weakSelf handleEndRefreshing:newCount];
    };
    
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        footerRefreshBlock(weakSelf.footerConfigBlock);
    }];
}

- (void)handleEndRefreshing:(NSInteger)count
{
    if (self.mj_header.isRefreshing) {
        [self.mj_header endRefreshing];
        [self.mj_footer endRefreshing];
    }
    
    if (count < [self.pageCount integerValue]) {
        [self.mj_footer endRefreshingWithNoMoreData];
    }else{
        [self.mj_footer endRefreshing];
    }
}

#pragma mark --结束刷新-----
//如果外部想自己处理结束刷新,可以为tabelView添加的其它结束刷新的方法。
- (void)beginRefreshing
{
    if (!self.mj_header) return;
    [self.mj_header beginRefreshing];
}

- (void)endRefrehing
{
    if (self.mj_header) {
        [self.mj_header endRefreshing];
    }
    if (self.mj_footer) {
        [self.mj_footer endRefreshing];
    }
}
@end
复制代码

External calls are as follows:

- (void)configTableView
{
    WeakSelf
    [self.tableView normalHeaderRefreshingActionBlock:^(FooterConfigBlock  _Nonnull footerConfig) {
        weakSelf.page = 1;
        //上拉刷新触发请求第一页数据。
        [weakSelf requestData:^(NSInteger count) {
            weakSelf.dataCount = count;
            [weakSelf.tableView reloadData];
            //请求成功后将数据的条数回调给tableView,让tableVie自己处理footer的设置
            footerConfig(count);
        } failCallBack:^{
            [weakSelf.tableView endRefrehing];
        }];
    }];

    [self.tableView backNormalFooterRefreshingActionBlock:^(FooterConfigBlock  _Nonnull footerConfig) {
        [weakSelf requestData:^(NSInteger count) {
            weakSelf.dataCount += count;
            [weakSelf.tableView reloadData];
            //请求成功后将数据的条数回调给tableView,让tableVie自己处理footer的设置
            footerConfig(count);
        } failCallBack:^{
            [weakSelf.tableView endRefrehing];
        }];
    }];
    
    [self.tableView beginRefreshing];
}

- (void)requestData:(void(^)(NSInteger count))successCallBack failCallBack:(void(^)(void))failCallBack
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (self.page > 5) {
            successCallBack(0);
        }else{
            successCallBack(20);
        }
        self.page += 1;
    });
}

复制代码

These are just examples of their simple package, MJRefresh provides a variety of header and footer, product or service according to the requirements of a custom package and use their own codes.

These are some of my use summary, if wrong, please correct me criticism. Thank you! ! !

Reproduced in: https: //juejin.im/post/5d025fb2e51d45773f2e8f41

Guess you like

Origin blog.csdn.net/weixin_33853794/article/details/93172648