引言
在日常開發中經常涉及資料串列的查詢,處理服務側無資料回傳的情況或者網路例外的手段是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逆向,只為你呈現有價值的資訊,專注于移動端技術研究領域,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/354680.html
標籤:其他
上一篇:使用Android Studio和阿里云資料庫實作一個遠程聊天程式
下一篇:Flutter涼了嗎?
