iOS小技能: 处理接口的暂无数据

Posted iOS逆向

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS小技能: 处理接口的暂无数据相关的知识,希望对你有一定的参考价值。

引言

在日常开发中经常涉及数据列表的查询,处理服务侧无数据返回的情况或者网络异常的手段是ios必备小技能。

I 处理暂无数据

网络请求失败,业务逻辑错误,返回数据为空都是需要处理界面的显示,推荐使用暂无数据进行提示。

1.1 用法

        if (weakSelf.viewModel.listDataArray.count == 0) 
            [weakSelf.viewModel.ShowNoviewSubject sendNext:QCTLocal(CRM_nodata_Info)];
            
        else
            [weakSelf.viewModel.hidenNoviewSubject sendNext:nil];
            
        

1.2 核心实现

V层初始化暂无数据视图:将视图添加到tableView,这样可以不影响下拉刷新和上拉加载

- (CRMNoDatatView *)NoView
    
    if (nil == _NoView) 
        
        CRMNoDatatView *tmpView = [[CRMNoDatatView alloc]init];
        
        _NoView = tmpView;
        [self.tableView addSubview:_NoView];
        
        __weak __typeof__(self) weakSelf = self;
        
        
        [_NoView mas_makeConstraints:^(MASConstraintMaker *make) 
            
            
            make.centerY.equalTo(weakSelf.tableView.mas_centerY).offset(kAdjustRatio(k_noteViewH));
            make.width.equalTo(weakSelf);
            
            
            
            make.left.right.bottom.equalTo(weakSelf.tableView);//tableView


            
        ];
        
        
        
    
    return _NoView;




- (void)ShowNoview:(NSString *)title img:(NSString*)imgName


    
    self.NoView.title = title;
    
    self.NoView.imgName = imgName;
    
    [self.tableView bringSubviewToFront:self.NoView];
    


V层监听C层的事件

    [self.viewModel.hidenNoviewSubject subscribeNext:^(id  _Nullable x) 
        weakSelf.NoView.hidden = YES;
    ];
    
    [self.viewModel.ShowNoviewSubject subscribeNext:^(id  _Nullable x) 
        weakSelf.NoView.hidden = NO;
        
        [weakSelf ShowNoview:x img:@"img_kongbai_zanwu"];

        
        
    ];

暂无数据视图的实现

// 显示暂无数据图片
- (UIImageView *)imageV
    if (nil == _imageV) 
        UIImageView *tmpView = [[UIImageView alloc]init];
        _imageV = tmpView;
        
        _imageV.contentMode = UIViewContentModeScaleAspectFit;

        _imageV.image = [UIImage imageNamed:@"icon_wushuju"];

        [self addSubview:_imageV];
        __weak __typeof__(self) weakSelf = self;

        
        [_imageV mas_makeConstraints:^(MASConstraintMaker *make) 
            make.centerX.equalTo(weakSelf);
            make.centerY.equalTo(weakSelf).offset(-kAdjustRatio(35));

            make.left.equalTo(weakSelf).offset(kAdjustRatio(33));
            
            make.right.equalTo(weakSelf).offset(kAdjustRatio(-33));
            
            

        ];
    
    return _imageV;



//显示暂无数据文本
- (UILabel *)label
    if (nil == _label) 
        UILabel *tmpView = [[UILabel alloc]init];
        _label = tmpView;
        [self addSubview:_label];
        __weak __typeof__(self) weakSelf = self;

        
        [_label mas_makeConstraints:^(MASConstraintMaker *make) 
            
            make.centerX.equalTo(weakSelf);
            make.top.equalTo(weakSelf.imageV.mas_bottom).offset(kAdjustRatio(22));
            
        
        _label.textAlignment = NSTextAlignmentCenter;

        
        _label.font = kPingFangFont(15);
        _label.textColor = rgb(51,51,51);

    
    return _label;





// 更新图片数据
-(void)setImgName:(NSString *)imgName
    _imgName = imgName;
    
    
    if (imgName.length<=0) 
        return;
    
    [self.imageV setImage:[UIImage imageNamed:imgName]];
    
    
        self.reloadbtnView.hidden = !self.isShowReloadBtn;
//    


- (void)setTitle:(NSString *)title
    _title = title;
    
    self.label.text = title;


see also

更多内容请关注#小程序:iOS逆向,只为你呈现有价值的信息,专注于移动端技术研究领域。

以上是关于iOS小技能: 处理接口的暂无数据的主要内容,如果未能解决你的问题,请参考以下文章

iOS小技能: app侧退出登录处理流程

iOS小技能: app侧退出登录处理流程

iOS小技能:封装银联接口协议(收银台的订单退款查询机制)下篇

iOS小技能:封装定位SDK,统一数据模型和错误处理。

iOS小技能:Socket基础知识

iOS小技能:简化版的隐私弹窗