//
// ViewController2.m
// TestTD
//
// Created by Mac on 2017/6/24.
// Copyright © 2017年 MLBiMAC. All rights reserved.
//
#import "ViewController2.h"
#import "UIScrollView+EmptyDataSet.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,getter=isLoading)BOOL loading;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =NO;
self.view.backgroundColor = [UIColorwhiteColor];
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
}
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,64,kScreenW, kScreenH -64)style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.viewaddSubview:_tableView];
_tableView.tableFooterView = [[UITableViewalloc]initWithFrame:CGRectZero];
}
return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *identifier =@"cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
}
cell.textLabel.text = [NSStringstringWithFormat:@"%ld",indexPath.row];
return cell;
}
/*
//返回单张图片
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
return [UIImage imageNamed:@"empty_placeholder"];
}
*/
//空白页显示图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
if (self.isLoading) {
// 圆形加载图片
return [UIImageimageNamed:@"loading_imgBlue_78x78"];
}else {
// 默认静态图片
return [UIImageimageNamed:@"empty_placeholder"];
}
}
//返回标题
-(NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
NSString *text =@"没有数据了!";
return [[NSAttributedStringalloc]initWithString:textattributes:nil ];
}
//返回详情
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
NSString *text =@"没有数据了!ahahahaahhaahahahahahahahahaha.";
NSMutableParagraphStyle *paragraph = [NSMutableParagraphStylenew];
paragraph.lineBreakMode =NSLineBreakByWordWrapping;
paragraph.alignment =NSTextAlignmentCenter;
NSDictionary *attributes =@{NSFontAttributeName: [UIFontsystemFontOfSize:15.0f],NSForegroundColorAttributeName: [UIColorlightGrayColor],NSParagraphStyleAttributeName: paragraph};
return [[NSAttributedStringalloc]initWithString:textattributes:attributes];
}
//返回可以点击的按钮上面带文字
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
NSDictionary *attributes =@{NSFontAttributeName: [UIFontboldSystemFontOfSize:17.0f]};
return [[NSAttributedStringalloc]initWithString:@"点击刷新"attributes:attributes];
}
/*
//返回可以点击的按钮上面带图片
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
return [UIImage imageNamed:@"search_icon"];
}
*/
//返回空白区域的颜色自定义
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{ return [UIColororangeColor];
}
- (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView{
NSLog(@"啦啦啦");
// 空白页面被点击时开启动画,reloadEmptyDataSet
self.loading =YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
// 关闭动画,reloadEmptyDataSet
self.loading =NO;
});
}
//图像视图动画:旋转
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"transform"];
animation.fromValue = [NSValuevalueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValuevalueWithCATransform3D:CATransform3DMakeRotation(M_PI_2,0.0,0.0, 1.0)];
animation.duration =0.25;
animation.cumulative =YES;
animation.repeatCount =MAXFLOAT;
return animation;
}
/*
//图像视图动画
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.duration = 1.25;
animation.cumulative = NO;
animation.repeatCount = MAXFLOAT;
animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 45, 45)];
return animation;
}
// 向代理请求图像视图动画权限。默认值为NO。
// 确保从 imageAnimationForEmptyDataSet返回有效的CAAnimation对象:
-(BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView{
return YES;
}
*/
//是否开启动画
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView {
return self.isLoading;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//空白页面被点击时刷新页面
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
// 空白页面被点击时开启动画,reloadEmptyDataSet
self.loading =YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
// 关闭动画,reloadEmptyDataSet
self.loading =NO;
});
}
//为该属性设置 setter方法,重新加载空数据集视图:
- (void)setLoading:(BOOL)loading
{
if (self.isLoading == loading) {
return;
}
_loading = loading;
[self.tableViewreloadEmptyDataSet];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
深入了解进入github:
https://github.com/dzenbot/DZNEmptyDataSet