zoukankan      html  css  js  c++  java
  • iOS三方-MJRefresh的使用

    MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单。github地址: https://github.com/CoderMJLee/MJRefresh 。

    下拉刷新

    官方给过来的例子很简单,默认使用如下:

    复制代码
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       // 进入刷新状态后会自动调用这个block
    }];
    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 马上进入刷新状态
    [self.tableView.header beginRefreshing];
    复制代码

    结束下拉刷新:

    // 拿到当前的下拉刷新控件,结束刷新状态
    [self.tableView.header endRefreshing];

    上拉刷新

    官方给过来的默认例子:

    复制代码
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       // 进入刷新状态后会自动调用这个block
    }];
    或
    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadMoreData方法)
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    复制代码

    结束上拉刷新:

    // 拿到当前的上拉刷新控件,结束刷新状态
    [self.tableView.footer endRefreshing];

    从上面,我们可以看到,一般情况下,进行页面的时候,我们会使用下拉刷新,并“马上进入刷新状态”,网络请求完数据后,结束“下拉刷新状态”。但上拉刷新,就不需要“马上进入刷新状态”了。

    更多使用例子,请参数官方给过来的例子,使用起来还是挺方便的。

    下面,给出某个项目的实际使用代码:

    复制代码
    //
    //  NJBillTableViewController.m
    //  NJWisdomCard
    //
    //  Created by admin on 15/8/21.
    //  Copyright (c) 2015年 Weconex. All rights reserved.
    //
    
    #import "NJBillTableViewController.h"
    #import "NJBillListTableviewCell.h"
    #import "Common.h"
    #import "NJHttpToolHandle.h"
    #import "MBProgressHUD+NJ.h"
    #import "NJAccountTool.h"
    #import "MJExtension.h"
    #import "MJRefresh.h"
    #import "NJAccountModel.h"
    #import "NJBillModel.h"
    
    @interface NJBillTableViewController()
    /**
     *  账单模型
     */
    @property (nonatomic, strong) NSMutableArray *billsFrames;
    /**
     *   页数
     */
    @property (nonatomic,assign) int pageIndex;
    
    @end
    
    @implementation NJBillTableViewController
    
    - (NSMutableArray *)billsFrames
    {
        if (!_billsFrames) {
            self.billsFrames = [NSMutableArray array];
        }
        return _billsFrames;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //集成下拉刷新控件
        [self setupDownRefresh];
        
        //集成上拉刷新控件
        [self setupUpRefresh];
    }
    
    /**
     *  集成上拉刷新控件
     */
    - (void)setupUpRefresh
    {
         self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)];
    }
    
    /**
     *  集成下拉刷新控件
     */
    - (void)setupDownRefresh
    {
        // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewBills)];
        
        // 马上进入刷新状态
        [self.tableView.header beginRefreshing];
    }
    /**
     *  加载下拉刷新数据
     */
    - (void)loadNewBills
    {
        _pageIndex=1;//默认加载第一页
        [self.billsFrames removeAllObjects];//移除所有的数据
        
        //1.从沙盒里拿用户模型
        NJAccountModel *accountModel=[NJAccountTool accountModel];
        
        // 2.拼接请求参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"userId"] =accountModel.loginId;//登录号
        params[@"page"] =[NSString stringWithFormat:@"%d",_pageIndex];//页数
        params[@"pagesize"] =[NSString stringWithFormat:@"10"];
        
        //3.发送请求
        [NJHttpToolHandle postWithServiceCode:@"queryAllBalance" params:params success:^(id responseObject) {
            if ([responseObject[@"resultCode"] isEqualToString:@"0000"]) {
                
                //获取value数组
                NSDictionary *dictList = [NSJSONSerialization JSONObjectWithData:[responseObject[@"code"] JSONData] options:NSJSONReadingMutableLeaves error:nil];
             
                // 将 "账单字典"数组 转为 "账单模型"数组
                NSArray *newBills = [NJBillModel objectArrayWithKeyValuesArray:dictList[@"value"]];
                
                // 将最新的账单数据,添加到总数组的最前面
                NSRange range = NSMakeRange(0, newBills.count);
                NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
                [self.billsFrames insertObjects:newBills atIndexes:set];
                //[self.billsFrames insertObjects:newBills atIndex:0];
                //[self.billsFrames insertObjects:newBills atIndexes:0];
                
                // 刷新表格
                [self.tableView reloadData];
                
                // 拿到当前的下拉刷新控件,结束刷新状态
                [self.tableView.header endRefreshing];
            }
            else
            {
                [self.tableView.header endRefreshing];
            }
            
        } failure:^(NSError *error) {
            [self.tableView.header endRefreshing];
        }];
    }
    
    /**
     *  加载上拉刷新数据
     */
    -(void)loadMoreBills
    {
        //1.设置页数
        _pageIndex++;//默认加载第一页
        
        // 2.拼接请求参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"userId"] =[NJAccountTool accountModel].loginId;//登录号
        params[@"page"] =[NSString stringWithFormat:@"%d",_pageIndex];//页数
        params[@"pagesize"] =[NSString stringWithFormat:@"10"];
        
        //3.发送请求
        [NJHttpToolHandle postWithServiceCode:@"queryAllBalance" params:params success:^(id responseObject) {
            if ([responseObject[@"resultCode"] isEqualToString:@"0000"]) {
                
                //获取value数组
                NSDictionary *dictList = [NSJSONSerialization JSONObjectWithData:[responseObject[@"code"] JSONData] options:NSJSONReadingMutableLeaves error:nil];
                
                // 将 "账单字典"数组 转为 "账单模型"数组
                NSArray *newBills = [NJBillModel objectArrayWithKeyValuesArray:dictList[@"value"]];
                
                // 将更多的账单数据,添加到总数组的最后面
                [self.billsFrames addObjectsFromArray:newBills];
    
                // 刷新表格
                [self.tableView reloadData];
                
                // 拿到当前的下拉刷新控件,结束刷新状态
                [self.tableView.footer endRefreshing];
            }
            else
            {
                [self.tableView.footer endRefreshing];
            }
            
        } failure:^(NSError *error) {
            [self.tableView.footer endRefreshing];
        }];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table view data source
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NJBillListTableviewCell *cell = [NJBillListTableviewCell cellWithTableView:tableView];
        cell.billModel=self.billsFrames[indexPath.item];
        return cell;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.billsFrames.count;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 80;
    }
    
    @end
    本内容来自: 超越昨天(学无止境) - http://www.cnblogs.com/xvewuzhijing/
  • 相关阅读:
    9.4
    9.3
    9.2
    2016.9.1
    html,body
    prototype
    京东笔试题的一些小细节
    gulp安装过程中的问题。
    json
    双飞翼布局和圣杯布局
  • 原文地址:https://www.cnblogs.com/xvewuzhijing/p/4904629.html
Copyright © 2011-2022 走看看