天天看點

iOS小技能: 處理接口的暫無資料

引言

在日常開發中經常涉及資料清單的查詢,處理服務側無資料傳回的情況或者網絡異常的手段是iOS必備小技能。

I 處理暫無資料

網絡請求失敗,業務邏輯錯誤,傳回資料為空都是需要處理界面的顯示,推薦使用暫無資料進行提示。

iOS小技能: 處理接口的暫無資料

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];
    
}

           
[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