码迷,mamicode.com
首页 > 移动开发 > 详细

iOS无网络提示或无数据提示

时间:2018-05-18 14:19:55      阅读:1611      评论:0      收藏:0      [点我收藏+]

标签:ted   fork   time   err   width   cmd   return   eva   图片   

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

iOS无网络提示或无数据提示

标签:ted   fork   time   err   width   cmd   return   eva   图片   

原文地址:https://www.cnblogs.com/HFEL/p/9055321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!