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