DZNEmptyDataSet使用

DZNEmptyDataSet主要是解决tableView和collectView内容为空的占位图,自己来做会有点麻烦,使用它将会很好的解决.(肯定要有占位图的,不然太难看).

使用很简单.首先到github地址上下载:
https://github.com/dzenbot/DZNEmptyDataSet

//手动导入或者使用Pod都可以.

在你需要的控制器这么写.

#import "ViewController.h"
#import"UIScrollView+EmptyDataSet.h"

@interface ViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic , strong ) UITableView * tableView ;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
_tableView.emptyDataSetSource=self;
_tableView.emptyDataSetDelegate=self;

// _tableView.dataSource=self;
// _tableView.delegate=self;
_tableView.tableFooterView=[UIView new];

[self.view addSubview:_tableView];

}

首先导入头文件,其他和使用tableView一样,遵守代理和数据源协议.
然后在数据源里设置数据,在代理里处理事件.

我现在把所有的数据源和代理发上来.首先是数据源.

@protocol DZNEmptyDataSetSource <NSObject>
@optional //可选,一个不实现都没有关系.但是任何效果都没有.

/**
Asks the data source for the title of the dataset.
The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.

@param scrollView A scrollView subclass informing the data source.
@return An attributed string for the dataset title, combining font, text color, text pararaph style, etc.
//返回一个富文本标题
*/

  • (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView;

/**
Asks the data source for the description of the dataset.
The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.

@param scrollView A scrollView subclass informing the data source.
@return An attributed string for the dataset description text, combining font, text color, text pararaph style, etc.
*/
//详情文字

  • (nullable NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView;

/**
Asks the data source for the image of the dataset.

@param scrollView A scrollView subclass informing the data source.
@return An image for the dataset.
*/
//设置图片

  • (nullable UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView;

/**
Asks the data source for a tint color of the image dataset. Default is nil.

@param scrollView A scrollView subclass object informing the data source.
@return A color to tint the image of the dataset.
*/
//图片 tint色

  • (nullable UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView;

/**

  • Asks the data source for the image animation of the dataset.
  • @param scrollView A scrollView subclass object informing the delegate.
  • @return image animation
    */
    //图片的动画效果
  • (nullable CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView;

/**
Asks the data source for the title to be used for the specified button state.
The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.

@param scrollView A scrollView subclass object informing the data source.
@param state The state that uses the specified title. The possible values are described in UIControlState.
@return An attributed string for the dataset button title, combining font, text color, text pararaph style, etc.

//指定button的文字
*/

  • (nullable NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;

/**
Asks the data source for the image to be used for the specified button state.
This method will override buttonTitleForEmptyDataSet:forState: and present the image only without any text.

@param scrollView A scrollView subclass object informing the data source.
@param state The state that uses the specified title. The possible values are described in UIControlState.
@return An image for the dataset button imageview.
*/
//按钮图片(设置了将不会有按钮文本)

  • (nullable UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;

/**
Asks the data source for a background image to be used for the specified button state.
There is no default style for this call.

@param scrollView A scrollView subclass informing the data source.
@param state The state that uses the specified image. The values are described in UIControlState.
@return An attributed string for the dataset button title, combining font, text color, text pararaph style, etc.
*/
//设置按钮的背景图片

  • (nullable UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state;

/**
Asks the data source for the background color of the dataset. Default is clear color.

@param scrollView A scrollView subclass object informing the data source.
@return A color to be applied to the dataset background view.
*/
//设置背景颜色

  • (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView;

/**
//自定义view
Asks the data source for a custom view to be displayed instead of the default views such as labels, imageview and button. Default is nil.
Use this method to show an activity view indicator for loading feedback, or for complete custom empty data set.
Returning a custom view will ignore -offsetForEmptyDataSet and -spaceHeightForEmptyDataSet configurations.
返回的view将不再管-offsetForEmptyDataSet and -spaceHeightForEmptyDataSet configurations里面的设置了.
@param scrollView A scrollView subclass object informing the delegate.
@return The custom view.
*/

  • (nullable UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView;

/**
Asks the data source for a offset for vertical and horizontal alignment of the content. Default is CGPointZero.

@param scrollView A scrollView subclass object informing the delegate.
@return The offset for vertical and horizontal alignment.
*/
//设置垂直和水平对齐间距

  • (CGPoint)offsetForEmptyDataSet:(UIScrollView *)scrollView DZNEmptyDataSetDeprecated(-verticalOffsetForEmptyDataSet:);
  • (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView;

/**
Asks the data source for a vertical space between elements. Default is 11 pts.

@param scrollView A scrollView subclass object informing the delegate.
@return The space height between elements.
*/
//元素与元素之间的垂直空白间距.

  • (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView;

@end

/////////////////////////////////

讲完了datesource,那就来看delegate了.

@protocol DZNEmptyDataSetDelegate <NSObject>

@optional

/**
Asks the delegate to know if the empty dataset should fade in when displayed. Default is YES.

@param scrollView A scrollView subclass object informing the delegate.
@return YES if the empty dataset should fade in.
*/
//是否渐进显示view,在显示之前你可以做你想要做的事.

  • (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView;

/**
Asks the delegate to know if the empty dataset should still be displayed when the amount of items is more than 0. Default is NO

@param scrollView A scrollView subclass object informing the delegate.
@return YES if empty dataset should be forced to display
*/
//当视图有数据了是否继续显示占位图,默认是不显示的.

  • (BOOL)emptyDataSetShouldBeForcedToDisplay:(UIScrollView *)scrollView;

/**
Asks the delegate to know if the empty dataset should be rendered and displayed. Default is YES.

@param scrollView A scrollView subclass object informing the delegate.
@return YES if the empty dataset should show.
*/
//是否占位图应该被渲染和重绘.

  • (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView;

/**
Asks the delegate for touch permission. Default is YES.

@param scrollView A scrollView subclass object informing the delegate.
@return YES if the empty dataset receives touch gestures.
*/
//是否一直允许触摸

  • (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView;

/**
Asks the delegate for scroll permission. Default is NO.

@param scrollView A scrollView subclass object informing the delegate.
@return YES if the empty dataset is allowed to be scrollable.
*/
//是否允许滚动.

  • (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView;

/**
Asks the delegate for image view animation permission. Default is NO.
Make sure to return a valid CAAnimation object from imageAnimationForEmptyDataSet:

@param scrollView A scrollView subclass object informing the delegate.
@return YES if the empty dataset is allowed to animate
*/
//是否允许动画.

  • (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView;

/**
Tells the delegate that the empty dataset view was tapped.
Use this method either to resignFirstResponder of a textfield or searchBar.

@param scrollView A scrollView subclass informing the delegate.
*/
//空白部分被点击了

  • (void)emptyDataSetDidTapView:(UIScrollView *)scrollView DZNEmptyDataSetDeprecated(-emptyDataSet:didTapView:);

/**
Tells the delegate that the action button was tapped.

@param scrollView A scrollView subclass informing the delegate.
*/
//按钮被点击了 .

  • (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView DZNEmptyDataSetDeprecated(-emptyDataSet:didTapButton:);

/**
Tells the delegate that the empty dataset view was tapped.
Use this method either to resignFirstResponder of a textfield or searchBar.

@param scrollView A scrollView subclass informing the delegate.
@param view the view tapped by the user
*/
//指定的view被点击了.

  • (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view;

/**
Tells the delegate that the action button was tapped.

@param scrollView A scrollView subclass informing the delegate.
@param button the button tapped by the user
*/
//指定的按钮被点击了

  • (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button;

/**
Tells the delegate that the empty data set will appear.

@param scrollView A scrollView subclass informing the delegate.
*/
//占位图即将显示

  • (void)emptyDataSetWillAppear:(UIScrollView *)scrollView;

/**
Tells the delegate that the empty data set did appear.

@param scrollView A scrollView subclass informing the delegate.
*/
//占位图已经显示

  • (void)emptyDataSetDidAppear:(UIScrollView *)scrollView;

/**
Tells the delegate that the empty data set will disappear.

@param scrollView A scrollView subclass informing the delegate.
*/
//占位图即将消失

  • (void)emptyDataSetWillDisappear:(UIScrollView *)scrollView;

/**
Tells the delegate that the empty data set did disappear.

@param scrollView A scrollView subclass informing the delegate.
*/
//占位图已经消失

  • (void)emptyDataSetDidDisappear:(UIScrollView *)scrollView;

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容