This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

The introduction

In daily development, it often involves the query of data list. It is a necessary skill for iOS to deal with the situation that no data is returned from the service side or the means of network exception.

I Processing no data temporarily

Network request failure, service logic error, and returned data are displayed on the processing interface. It is recommended that no data be displayed.

1.1 usage

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

Copy the code

1.2 Core Implementation

Layer V initializing temporary no data view: Add the view to the tableView, so that it can not affect the drop-down refresh and the drop-down load

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

Copy the code

Layer V listens for layer C events

    [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"];

        
        
    }];

Copy the code

Implementation of no data view at present

// No data is displayed
- (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;
}


// Displays no data text temporarily
- (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;
}




// Update image data- (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;
}

Copy the code

see also

For more, check out # Applets: iOS Reverse, which presents valuable information only for you, focusing on the mobile technology research field.