DZNEmptyDataSet(UITableView空数据占位)
DZNEmptyDataSet
是一个非常好用的UITableView
或者UICollectionView
空数据时的占位视图的一个第三方,只需要两个文件就可以搞定。 原文链接DZNEmptyDataSet
1. Demo演示

-

3. 步骤
- 引入头文件
#import "UIScrollView+EmptyDataSet.h"
self.baseTableView.emptyDataSetDelegate = self;
self.baseTableView.emptyDataSetSource = self;
self.baseTableView.tableFooterView = [UIView new];
[self.baseTableView reloadData];
[self.baseTableView reloadEmptyDataSet];
- 设置
DZNEmptyDataSetSource
(下面会把所有的方法都罗列出来配上说明可以根据自己的需求去设置这些代理,都是可选的) - (1)设置标题
- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView;
/**
Demo 演示中的标题, 返回的是 NSAttributedString, 可以根据自己的需求设置字体的格式
*/
- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView;
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"标题标题标题标题标题标题";
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0],
NSForegroundColorAttributeName: [UIColor colorWithRed:170/255.0 green:171/255.0 blue:179/255.0 alpha:1.0],
NSParagraphStyleAttributeName: paragraphStyle};
return [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
}
- (2)设置副标题
- (nullable NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
/**
Demo 演示中的副标题, 返回的是 NSAttributedString, 可以根据自己的需求设置字体的格式
例如分行,大小,颜色 等等
*/
- (nullable NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView;
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"副标题副标题副标题副标题副标题副标题副标题副标题\n\nTo 也是副标题也是副标题也是副标题也是副标题也是副标题";
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.0],
NSForegroundColorAttributeName: [UIColor colorWithRed:170/255.0 green:171/255.0 blue:179/255.0 alpha:1.0],
NSParagraphStyleAttributeName: paragraphStyle};
return [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
}
- (3) 设置图片
- (nullable UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView;
/**
设置占位视图中的图片,返回一个UIImage
*/
- (nullable UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView;
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
return [UIImage imageNamed:@"001"];
}
- (4) 设置图片上的颜色
- (nullable UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView;
/**
设置图片上的颜色,默认值是nil,返回一个 UIColor (一般情况下用不到)
*/
- (nullable UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView;
- (nullable UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView
{
return [UIColor redColor];
}
- (5) 设置占位视图的背景色
- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView;
/**
设置占位视图的背景色,返回一个 UIColor ,默认是Clear
*/
- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView;
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
return [UIColor whiteColor];
}
- (6) 设置占位视图中图片的动画
- (nullable CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView;
/**
设置占位视图中图片的动画 ,返回一个 CAAnimation,但必须 DZNEmptyDataSetDelegate 中的
emptyDataSetShouldAnimateImageView: 返回YES , 动画才有效
*/
- (nullable CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView;
- (nullable 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
}
- (7) 设置底部Button上的文本
- (nullable NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
/**
设置不同状态下Button上的文本
*/
- (nullable NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
- (nullable NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
NSString *text = @"底部的Button";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],NSForegroundColorAttributeName: [UIColor darkGrayColor]};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (8) 设置底部Button的图片
- (nullable UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
- (nullable UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
- (nullable UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
return [UIImage imageNamed:@"004"];
}
- (9) 设置底部Button的背景图片
- (nullable UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
- (nullable UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;
- (nullable UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;
- (nullable UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
return [UIImage imageNamed:@"004"];
}
- (10) 自定义占位视图
- (nullable UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView;
- (nullable UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor whiteColor];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 10, 60, 30);
btn.backgroundColor = [UIColor orangeColor];
[view addSubview:btn];
return view;
}
- (11) 设置占位视图居中y轴的偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
return 0;
}
- (12) 设置标题之间的间距
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
return 0;
}
- 设置
DZNEmptyDataSetDelegate
- (1) 显示 空数据源的时候 要淡入的效果
- (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView;
- (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView;
- (2) 在不是空数据源时 要不要显示占位视图
- (BOOL)emptyDataSetShouldBeForcedToDisplay:(UIScrollView *)scrollView;
- (BOOL)emptyDataSetShouldBeForcedToDisplay:(UIScrollView *)scrollView;
- (3) 在是空数据的时候 是否显示占位视图
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView;
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView;
- (4) 是否允许点击
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView;
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView;
- (5) 占位视图 是否可以滚动
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView;
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView;
- (6) 是否开启动画
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView;
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView;
- (7) 点击 占位视图中的图片以及文本信息
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view;
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view;
- (8) 点击 占位视图中底部的Button
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button;
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button;
- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView;
- (void)emptyDataSetDidAppear:(UIScrollView *)scrollView;
- (void)emptyDataSetWillDisappear:(UIScrollView *)scrollView;
- (void)emptyDataSetDidDisappear:(UIScrollView *)scrollView;
完结,喜欢就给个赞,没有源码,写的这么清楚,这么简单应该也用不到demo吧!