iOS開(kāi)發(fā)之無(wú)網(wǎng)絡(luò)數(shù)據(jù)時(shí)候占位圖

哎,連續(xù)加班加了一個(gè)多月,身心俱疲。每晚一兩點(diǎn)回去睡覺(jué),到今天終于開(kāi)熬出頭了。。。后臺(tái)的幾個(gè)在忙著部署服務(wù)器,我就在這里偷閑,正好把最近用到的一個(gè)非常非常實(shí)用的工具拿出來(lái)分享一下。

APP中不可避免地要用到tableView,但是在網(wǎng)絡(luò)差或者沒(méi)有網(wǎng)絡(luò)的情況下,tableView沒(méi)有數(shù)據(jù)會(huì)是一片空白,非常的丑。。。。這時(shí)候就需要用一個(gè)占位圖來(lái)填充和美化。以前的時(shí)候做占位圖的時(shí)候,自己傻不愣登的先去監(jiān)聽(tīng)看看有沒(méi)有數(shù)據(jù),然后再根據(jù)情況來(lái)決定填上占位圖還是用刷新數(shù)據(jù),既繁瑣還惡心,一不留神還會(huì)報(bào)錯(cuò)。后來(lái)在一個(gè)iOS群里面,一位道友給推薦了一個(gè)專(zhuān)門(mén)處理這種問(wèn)題的第三方庫(kù),那就是DZNEmptyDataSet,一用之下果然很犀利。好東西不敢獨(dú)享,特地拿出來(lái)分享一下。我才用了幾天時(shí)間,對(duì)這個(gè)框架認(rèn)識(shí)還很粗淺,如果有錯(cuò)請(qǐng)各位大濕指正。

首先認(rèn)識(shí)一下這個(gè)庫(kù)

github下載地址DZNEmptyDataSet

就像這樣子的占位圖(偷了他主頁(yè)上)


2016122262177Snip20161222_1.png

這個(gè)庫(kù)主要用于UITableView和UICollectionView上的,也可以用于UIScrollView(不過(guò)我沒(méi)有用過(guò))。

使用方法。

使用過(guò)程非常的簡(jiǎn)單。

  • 首先去下載后拖入到工程中,推薦使用cocoapods,因?yàn)檫@個(gè)不像支付 分享那些SDK那么惡心,cocoapods容易出錯(cuò)(像支付分享那些,個(gè)人真心推薦直接拖進(jìn)去,不要用cocoapods,不然很容易出錯(cuò)的)。

  • 在用到的地方導(dǎo)入這個(gè)庫(kù),肯定每個(gè)地方都用得到,所以還是添加到pch中比較方便

#import "UIScrollView+EmptyDataSet.h"
  • 使用步驟非常簡(jiǎn)單:
    1. 引入這個(gè)庫(kù)的兩個(gè)協(xié)議。

@interface WKMessageController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>


2. 設(shè)置tableView代理

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
//去掉TableView中的默認(rèn)橫線
self.tableView.tableFooterView = [UIView new];

3. 設(shè)置如數(shù)據(jù)時(shí)候的占位圖片
#pragma mark - 無(wú)數(shù)據(jù)占位
//無(wú)數(shù)據(jù)占位
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  return [UIImage imageNamed:@"pic_networkerror"];
}

  • (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    return animation;
    }
就這樣簡(jiǎn)單的幾步,一個(gè)占位圖就做好了。
![2016122274652QQ20161222-0.png](http://upload-images.jianshu.io/upload_images/1377427-9704956b66883fa6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

### 擴(kuò)展設(shè)置
- 設(shè)置文字(上圖下面的文字,我這個(gè)圖片默認(rèn)沒(méi)有這個(gè)文字的)是富文本樣式,擴(kuò)展性很強(qiáng)!

//這個(gè)是設(shè)置標(biāo)題文字的

  • (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"網(wǎng)絡(luò)不給力";

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
    NSForegroundColorAttributeName: [UIColor darkGrayColor]};

    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
    }


  //這是設(shè)置內(nèi)容描述的
  - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView { 
      NSString *text = @"請(qǐng)檢查網(wǎng)絡(luò)后再重試"; 
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new]; 
        paragraph.lineBreakMode = NSLineBreakByWordWrapping; 
        paragraph.alignment = NSTextAlignmentCenter; 
        NSDictionary *attributes = @{
                                              NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                              NSForegroundColorAttributeName: [UIColor lightGrayColor], 
                                              NSParagraphStyleAttributeName: paragraph
                                      }; 

return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- 設(shè)置按鈕的文本和按鈕的背景圖片
  • (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSLog(@"buttonTitleForEmptyDataSet:點(diǎn)擊上傳照片");
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};
    return [[NSAttributedString alloc] initWithString:@"點(diǎn)擊上傳照片" attributes:attributes];
    }
  • (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    return [UIImage imageNamed:@"button_image"];
    }
- 設(shè)置占位圖空白頁(yè)的背景色( 圖片優(yōu)先級(jí)高于文字)
  • (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor whiteColor];
    }
- 如果不嫌麻煩,還可以自定義一張自己的view放上去  我是嫌麻煩。。。。
  • (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView;

- 如果有頭視圖的話,光這樣 頭視圖有可能會(huì)擋住,這回事就需要跳轉(zhuǎn)位置了
  • (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return KSCREEN_HEIGHT * 0.5f;

}


- 當(dāng)然還可以添加占位圖的點(diǎn)擊時(shí)間,點(diǎn)擊了重新請(qǐng)求
//空白頁(yè)點(diǎn)擊事件
- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
   //重新加載默認(rèn)的數(shù)據(jù)
  NSDictionary *param = @{@"page":@"1",
                        @"epage":@"20"
                        };
  [self loadDataWith:param];
}


- 最后還能控制各種屬性
//是否顯示空白頁(yè),默認(rèn)YES
  • (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
    }
    //是否允許點(diǎn)擊,默認(rèn)YES
  • (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return NO;
    }
    //是否允許滾動(dòng),默認(rèn)NO
  • (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
    }
    //圖片是否要?jiǎng)赢?huà)效果,默認(rèn)NO
  • (BOOL) emptyDataSetShouldAllowImageViewAnimate:(UIScrollView *)scrollView {
    return YES;
    }
    //空白頁(yè)點(diǎn)擊事件
  • (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    return YES;
    }
    //空白頁(yè)按鈕點(diǎn)擊事件
  • (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView {
    return YES;
    }





最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容