App經(jīng)常在tableView沒有數(shù)據(jù)的時(shí)候需要顯示,占位圖,和暫無數(shù)據(jù)的文字,如下圖一樣

FzM7fm2.png!web.png
github鏈接: https://github.com/dzenbot/DZNEmptyDataSet
提示:主要用于UITableView和UICollectionView,也可以用于UIScrollView,其實(shí)主要是前兩個(gè)會用到空白或者網(wǎng)絡(luò)出錯(cuò)頁,采用給UIScrollView添加代理方法來給頁面添加空白頁面,源碼很有學(xué)習(xí)意義。
首先導(dǎo)入工程:
pod 'DZNEmptyDataSet'
或手動導(dǎo)入,下載文件,將Source文件下下的文件拖入工程
#import "UIScrollView+EmptyDataSet.h"
初始化
引入代理
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
讓tableview遵守代理
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
//這行代碼必須加上,可以去除tableView的多余的線,否則會影響美觀
self.tableView.tableFooterView = [UIView new];
常用代理方法
1.返回圖片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
return [UIImage imageNamed:@"empty_placeholder"];
}
2.返回標(biāo)題文本
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"Please Allow Photo Access";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
3.返回詳細(xì)描述
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
NSString *text = @"This allows you to share photos from your library and save photos to your camera roll.";
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];
}
4.按鈕文本或者背景樣式
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};
return [[NSAttributedString alloc] initWithString:@"Continue" attributes:attributes];
}
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
return [UIImage imageNamed:@"button_image"];
}
5.空白頁的背景色
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
return [UIColor whiteColor];
}
6.也可以自定義
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//加入你自定義的view
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityView startAnimating];
return activityView;
}
7.其他需求
//是否顯示空白頁,默認(rèn)YES
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
return YES;
}
//是否允許點(diǎn)擊,默認(rèn)YES
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
return YES;
}
//是否允許滾動,默認(rèn)NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
return YES;
}
//圖片是否要動畫效果,默認(rèn)NO
- (BOOL) emptyDataSetShouldAllowImageViewAnimate:(UIScrollView *)scrollView {
return YES;
}
//空白頁點(diǎn)擊事件
- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
}
//空白頁按鈕點(diǎn)擊事件
- (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView {
}