• 下拉上拉细节处理


    利用一个下面这个例子,来解决在使用刷新控件时网络延迟问题

    这是一个tableView显示网络请求的数据

    1.相关属性

    @interface DDZJokeViewController ()
    
    /** 获取的数据 */
    @property (nonatomic,strong) NSMutableArray *jokeData;
    
    /** 存储maxtime */
    @property (nonatomic,copy) NSString *maxTime;
    
    /** 页码 */
    @property (nonatomic,assign) NSInteger page;
    
    /** 存储上一次的请求 */
    @property (nonatomic,strong) NSDictionary *params;
    
    @end

    2.初始化

    - (NSMutableArray *)jokeData {
        if (!_jokeData) {
            _jokeData = [NSMutableArray array];
        }
        return _jokeData;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //加载刷新控件
        [self loadRefresh];
    }

    3.相关的方法

    /**
     * 加载刷新控件
     */
    - (void)loadRefresh {
        //设置默认下拉刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewJoke)];
        //根据拖拽比例自动切换透明度
        self.tableView.mj_header.automaticallyChangeAlpha = YES;
        
        //一进入就加载
        [self.tableView.mj_header beginRefreshing];
        
        //设置上拉刷新
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreJoke)];
    }

    因为上拉和下拉同时存在,所以,你要考虑到在网络延迟时,用户可能会同时做这两个操作

    需要处理这些细节:

    //加载下拉段子
    - (void)loadNewJoke {
       
        //先结束上拉
        [self.tableView.mj_footer endRefreshing];
        
        //请求参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"a"] = @"list";
        params[@"c"] = @"data";
        params[@"type"] = @29;
        
        self.params = params;
        
        //加载数据
        [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
            
            //当数据返回回来时,发现请求参数已经更改,那就直接返回
            if(self.params != params) return;
            
            //存下maxtime
            self.maxTime = responseObject[@"info"][@"maxtime"];
            
            //字典->模型
            self.jokeData = [DDZJoke mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            
            //刷新数据
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            
            //页码
            self.page = 0;
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if(self.params != params) return;
            [self.tableView.mj_header endRefreshing];
        }];
    }

    上拉下拉都要将请求参数进行一次保存,在数据返回时进行一次判断,如果请求参数发生改变了,那就将这次的请求结果丢弃

    //加载上拉段子
    - (void)loadMoreJoke {
        
        //先结束下拉
        [self.tableView.mj_header endRefreshing];
        
        
        self.page++;
        
        //请求参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"a"] = @"list";
        params[@"c"] = @"data";
        params[@"type"] = @29;
        params[@"page"] = @(self.page);
        params[@"maxtime"] = self.maxTime;
        
        self.params = params;
        
        //加载数据
        [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
            
            
            if(self.params != params) return;
            
            //存下maxtime
            self.maxTime = responseObject[@"info"][@"maxtime"];
            
            //字典->模型
            NSArray *newJoke = [DDZJoke mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            [self.jokeData addObjectsFromArray:newJoke];
            
            //刷新数据
            [self.tableView reloadData];
            
            [self.tableView.mj_footer endRefreshing];
        
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            self.page--;
            if(self.params != params) return;
            [self.tableView.mj_footer endRefreshing];
        }];
    }

    4.tableView的代理方法

    #pragma mark - <UITableViewDataSource>
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.jokeData.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString * ID = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        
        DDZJoke *dict = self.jokeData[indexPath.row];
        cell.textLabel.text = dict.name;
        cell.detailTextLabel.text = dict.text;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:dict.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
        
        return cell;
    }
  • 相关阅读:
    Oracle 服务命名(别名)的配置及原理,plsql连接用
    AdHoc发布时出现重复Provisioning Profile的解决方案
    xcode5时代如何设置Architectures和Valid Architectures
    C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
    DataTable的数据批量写入数据库
    高中生活--第7篇–我为什么不交作业
    ITFriend网站内测公测感悟
    网站推广第一周总结和反思
    第一次当面试官
    技术人才的出路在哪里,5种选择和2种思路
  • 原文地址:https://www.cnblogs.com/langji/p/5487000.html
Copyright © 2020-2023  润新知