信号不好网络错误的时候,一般都会有个提示:“网络错误请点击重试~” 的话术,或者说当你浏览某一页的时候,没有数据,也会提示:“暂无数据,请搞点动静” 之类的话术。 下面写了一个空白页(配合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];