欢迎使用CSDN-markdown编辑器

来源:互联网 发布:越狱后软件源 编辑:程序博客网 时间:2024/06/05 14:20

DZNEmptyDataSet(UITableView空数据占位)

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

  • 1. Demo演示

    123.gif

  • 2. 只需要UIScrollView+EmptyDataSet.hUIScrollView+EmptyDataSet.m 两个文件拖到项目中即可

    屏幕快照 2017-06-22 下午2.27.11.png

  • 3. 步骤
  • 引入头文件 #import "UIScrollView+EmptyDataSet.h"
// 遵守 emptyDataSetDelegate 和 emptyDataSetSource  代理 self.baseTableView.emptyDataSetDelegate = self;self.baseTableView.emptyDataSetSource = self;// 设置 tableFooterView 的目的是为了隐藏 多余的线 不然空数据的时候显示占位视图 上面会有线self.baseTableView.tableFooterView = [UIView new];
  • ###[!!! 不要忘记刷新]
//  reloadData 的时候 也要刷新占位视图  reloadEmptyDataSet [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;
// 设置底部 button 的图片,此方法将覆盖buttonTitleForEmptyDataSet:forState:并且只显示没有任何文本的图像。 - (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;
// 设置底部 button 的背景图片 - (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;
// 自定义占位视图,可以不用默认是模板,自己定义一个个性化的视图 // 返回自定义视图将忽略-offsetForEmptyDataSet和-spaceHeightForEmptyDataSet配置。 - (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;
// 设置占位视图居中y轴的偏移量 , 默认是 0  - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView;
 - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{    return 0;}
  • (12) 设置标题之间的间距- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;
// 设置标题之间的间距 , 默认是 11 pts - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;
 - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{    return 0;}
  • 设置 DZNEmptyDataSetDelegate
  • (1) 显示 空数据源的时候 要淡入的效果 - (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView;
// 显示 空数据源的时候 要淡入的效果 默认值 是 YES - (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView;
  • (2) 在不是空数据源时 要不要显示占位视图 - (BOOL)emptyDataSetShouldBeForcedToDisplay:(UIScrollView *)scrollView;
// 在不是空数据源时 要不要显示占位 默认值 NO (如果为YES TableView 会不能滑动) - (BOOL)emptyDataSetShouldBeForcedToDisplay:(UIScrollView *)scrollView;
  • (3) 在是空数据的时候 是否显示占位视图 - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView;
// 在是空数据的时候 是否显示占位 默认是 YES - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView;
  • (4) 是否允许点击 - (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView;
// 是否允许点击 默认是 YES -  (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView;
  • (5) 占位视图 是否可以滚动 - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView;
// 占位视图 是否可以滚动 默认是 NO - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView;
  • (6) 是否开启动画 - (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView;
// 是否开启动画 默认是 NO (if YES 确保imageAnimationForEmptyDataSet: 返回一个有效的动画) - (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;
// 点击 占位视图中底部的Button - (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button;
  • (9) 占位视图的生命周期
 - (void)emptyDataSetWillAppear:(UIScrollView *)scrollView; // 视图将要出现 - (void)emptyDataSetDidAppear:(UIScrollView *)scrollView;  // 视图已经出现 - (void)emptyDataSetWillDisappear:(UIScrollView *)scrollView; // 视图将要消失 - (void)emptyDataSetDidDisappear:(UIScrollView *)scrollView; // 视图已经消失
完结,喜欢就给个赞,没有源码,写的这么清楚,这么简单应该也用不到demo吧!
原创粉丝点击