引言
在日常开发中经常涉及数据列表的查询,处理服务侧无数据返回的情况或者网络异常的手段是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;
}