iOS无网络提示或无数据提示
Posted hfel
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS无网络提示或无数据提示相关的知识,希望对你有一定的参考价值。
信号不好网络错误的时候,一般都会有个提示:“网络错误请点击重试~” 的话术,或者说当你浏览某一页的时候,没有数据,也会提示:“暂无数据,请搞点动静” 之类的话术。 下面写了一个空白页(配合masonry),直接把分类拖进去用可以了
先看效果图:
1.网络错误
2.无数据
上菜:
建一个UIView的分类 利用runtime动态添加属性的方法实现空白视图效果
@class CDMNetErrorPageView , CDMBlankPageView; @interface UIView (CDMEmpty) //CDMNetErrorPageView @property (nonatomic,strong) CDMNetErrorPageView * netErrorPageView; - (void)configReloadAction:(void(^)())block; - (void)showNetErrorPageView; - (void)hideNetErrorPageView; //CDMBlankPageView @property (nonatomic,strong) CDMBlankPageView* blankPageView; - (void)showBlankPageView; - (void)hideBlankPageView; @end #pragma mark --- CDMNetErrorPageView @interface CDMNetErrorPageView : UIView @property (nonatomic,copy) void(^didClickReloadBlock)(); @end #pragma mark --- CDMBlankPageView @interface CDMBlankPageView : UIView @end
核心代码:
@interface UIView () @property (nonatomic,copy) void(^reloadAction)(); @end @implementation UIView (CDMEmpty) - (void)setReloadAction:(void (^)())reloadAction{ objc_setAssociatedObject(self, @selector(reloadAction), reloadAction, OBJC_ASSOCIATION_COPY); } - (void (^)())reloadAction{ return objc_getAssociatedObject(self, _cmd); } //CDMNetErrorPageView - (void)setNetErrorPageView:(CDMNetErrorPageView *)netErrorPageView{ [self willChangeValueForKey:NSStringFromSelector(@selector(netErrorPageView))]; objc_setAssociatedObject(self, @selector(netErrorPageView), netErrorPageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self didChangeValueForKey:NSStringFromSelector(@selector(netErrorPageView))]; } - (CDMNetErrorPageView *)netErrorPageView{ return objc_getAssociatedObject(self, _cmd); } - (void)configReloadAction:(void (^)())block{ self.reloadAction = block; if (self.netErrorPageView && self.reloadAction) { self.netErrorPageView.didClickReloadBlock = self.reloadAction; } } - (void)showNetErrorPageView{ if (!self.netErrorPageView) { self.netErrorPageView = [[CDMNetErrorPageView alloc]initWithFrame:self.bounds]; if (self.reloadAction) { self.netErrorPageView.didClickReloadBlock = self.reloadAction; } } [self addSubview:self.netErrorPageView]; [self bringSubviewToFront:self.netErrorPageView]; } - (void)hideNetErrorPageView{ if (self.netErrorPageView) { [self.netErrorPageView removeFromSuperview]; self.netErrorPageView = nil; } } //CDMBlankPageView - (void)setBlankPageView:(CDMBlankPageView *)blankPageView{ [self willChangeValueForKey:NSStringFromSelector(@selector(blankPageView))]; objc_setAssociatedObject(self, @selector(blankPageView), blankPageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self didChangeValueForKey:NSStringFromSelector(@selector(blankPageView))]; } - (CDMBlankPageView *)blankPageView{ return objc_getAssociatedObject(self, _cmd); } - (void)showBlankPageView{ if (!self.blankPageView) { self.blankPageView = [[CDMBlankPageView alloc]initWithFrame:self.bounds]; } [self addSubview:self.blankPageView]; [self bringSubviewToFront:self.blankPageView]; } - (void)hideBlankPageView{ if (self.blankPageView) { [self.blankPageView removeFromSuperview]; self.blankPageView = nil; } } @end
调用:
// 网络错误时,展示错误页 [self.view showNetErrorPageView]; [self.view configReloadAction:^{ NSLog(@"刷新"); }]; //隐藏网络错误页 [self.view hideNetErrorPageView]; --------------------------------------------------------- //无数据展示空白页 [self.view showBlankPageView]; //无数据隐藏空白页 [self.view hideBlankPageView];
Demo地址:[email protected]:domanc/CDMEmptyPageView.git
以上是关于iOS无网络提示或无数据提示的主要内容,如果未能解决你的问题,请参考以下文章
2021-09-11:给你一个32位的有符号整数x,返回将x中的数字部分反转后的结果。反转后整数超过 32 位的有符号整数的范围就返回0,假设环境不允许存储 64 位整数(有符号或无符号)。(代码片段