iOS無網路提示或無資料提示

來源:互聯網
上載者:User

標籤: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地址:[email protected]:domanc/CDMEmptyPageView.git 

iOS無網路提示或無資料提示

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.