zoukankan      html  css  js  c++  java
  • iOS下拉刷新和上拉刷新

    在iOS开发中,我们经常要用到下拉刷新和上拉刷新来加载新的数据,当前这也适合分页。iOS原生就带有该方法,下面就iOS自带的下拉刷新方法来简单操作。

    上拉刷新

    1、在TableView里,一打开软件,我们就调用下拉刷新事件。

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 集成刷新控件
        [self setupRefresh];
        
    }
    
    /**
     *  集成下拉刷新
     */
    -(void)setupRefresh
    {
        //1.添加刷新控件
        UIRefreshControl *control=[[UIRefreshControl alloc]init];
        [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
        [self.tableView addSubview:control];
        
        //2.马上进入刷新状态,并不会触发UIControlEventValueChanged事件
        [control beginRefreshing];
        
        // 3.加载数据
        [self refreshStateChange:control];
    }
    

    2、接下来,我们就要实现 refreshStateChange 这个方法,在里面显示数据和关闭下拉刷新。

    /**
     *  UIRefreshControl进入刷新状态:加载最新的数据
     */
    -(void)refreshStateChange:(UIRefreshControl *)control
    {
        // 3.发送请求
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        
        [mgr GET:@"https://api.weibo.com/2/statuses/public_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject){
            
            //1.获取数据,处理数据,传递数据给tableView,如:
            
            // 将最新的微博数据,添加到总数组的最前面
    //        NSRange range = NSMakeRange(0, newStatuses.count);
    //        NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
    //        [self.statuses insertObjects:newStatuses atIndexes:set];
            
            //2.刷新表格
            [self.tableView reloadData];
            
            // 3. 结束刷新
            [control endRefreshing];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
            // 结束刷新刷新 ,为了避免网络加载失败,一直显示刷新状态的错误
            [control endRefreshing];
        }];
    }
    

    上拉刷新 

    上拉刷新,一般用于分页请求,拉到底后,自动加载下一页。下面就拿加载新浪微博数据为例。

    一、由于下载加载更多数据,是一个不变的布局控件,我们就用xib来实现。

    HWLoadMoreFooter.h

    #import <UIKit/UIKit.h>
    
    @interface HWLoadMoreFooter : UIView
    +(instancetype)footer;
    @end
    

    HWLoadMoreFooter.m

    #import "HWLoadMoreFooter.h"
    
    @implementation HWLoadMoreFooter
    
    +(instancetype)footer
    {
        return [[[NSBundle mainBundle] loadNibNamed:@"HWLoadMoreFooter" owner:nil options:nil] lastObject];
    }
    
    @end
    

    接着,我们建立一个名为HWLoadMoreFooter的xib

    接下来,需要设置下面三个地方:

    接着在框里拖拉一个Label,设置Label为填充整个view

    最后,点击下图红色框,Update Frames

    xib建好之后,下面我们来实现上拉刷新的代码

    二.实现代码。

    1.在TabelView中加载时,先加载该控件

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 集成下拉刷新控件
        [self setupUpRefresh];
        
        // 集成上拉刷新控件
        [self setupDownRefresh];
        
    }
    

     2.集成上拉刷新方法

    /**
     *  集成上拉刷新
     */
    -(void)setupDownRefresh
    {
        HWLoadMoreFooter *footer = [HWLoadMoreFooter footer];
        footer.hidden = YES;
        self.tableView.tableFooterView = footer;
    }
    

     3.异步请求数据方法

    - (void)loadMoreStatus
    {
        // 1.请求管理者
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        
        // 2.拼接请求参数
        HWAccount *account = [HWAccountTool account];
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"access_token"] = account.access_token;
        
        // 取出最后面的微博(最新的微博,ID最大的微博)
        HWStatus *lastStatus = [self.statuses lastObject];
        if (lastStatus) {
            // 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。
            // id这种数据一般都是比较大的,一般转成整数的话,最好是long long类型
            long long maxId = lastStatus.idstr.longLongValue - 1;
            params[@"max_id"] = @(maxId);
        }
        
        // 3.发送请求
        [mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
            // 将 "微博字典"数组 转为 "微博模型"数组
            NSArray *newStatuses = [HWStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
            
            // 将更多的微博数据,添加到总数组的最后面
            [self.statuses addObjectsFromArray:newStatuses];
            
            // 刷新表格
            [self.tableView reloadData];
            
            // 结束刷新(隐藏footer)
            self.tableView.tableFooterView.hidden = YES;
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            HWLog(@"请求失败-%@", error);
            
            // 结束刷新
            self.tableView.tableFooterView.hidden = YES;
        }];
    }
    

     4.实现scrollViewDidScroll

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        //    scrollView == self.tableView == self.view
        // 如果tableView还没有数据,就直接返回
        if (self.statuses.count == 0 || self.tableView.tableFooterView.isHidden == NO) return;
        
        CGFloat offsetY = scrollView.contentOffset.y;
        
        // 当最后一个cell完全显示在眼前时,contentOffset的y值
        CGFloat judgeOffsetY = scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.height - self.tableView.tableFooterView.height;
        if (offsetY >= judgeOffsetY) { // 最后一个cell完全进入视野范围内
            // 显示footer
            self.tableView.tableFooterView.hidden = NO;
            
            // 加载更多的微博数据
            [self loadMoreStatus];
        }
    }
    
  • 相关阅读:
    [daily][troubleshoot][archlinux][wps][font] wps文档中的图内容无法显示中文
    [troubleshoot][daily][archlinux][pacman] pacman 与 pip 包文件冲突
    [daily] 宇宙终极shell之zsh
    [knowledge][basic][hardware] 内存的硬件结构(转)
    [troubleshoot][archlinux][X] GPU HANG
    [daily]使用rdtsc指令,测量程序的运行速度 [转]
    [have_fun] 好玩哒小游戏又来啦
    [dpdk] 读开发指南(2)(内容长期整理中)
    [Virtualization][SDN] 讲的很好的SDN软件定义网络视频课程
    [Virtualization][SDN] VXLAN到底是什么 [转]
  • 原文地址:https://www.cnblogs.com/jys509/p/4499023.html
Copyright © 2011-2022 走看看