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地址:git@github.com:domanc/CDMEmptyPageView.git 

转载于:https://www.cnblogs.com/HFEL/p/9055321.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值