摘要: 主要用于UITableView和UICollectionView,UIScrollView,數(shù)據(jù)為空時,頁面一片空白,不是一個很好的用戶體驗(yàn)。我們都希望在數(shù)據(jù)源為空的時候,給用戶一些相應(yīng)的提示,DZNEmptyDataSet完美解決這個問題。
使用詳解
- 導(dǎo)入第三方,遵守協(xié)議
#import "UIScrollView+EmptyDataSet.h"
遵守 <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate> 協(xié)議
- 設(shè)置代理
self.mainTableView.emptyDataSetSource = self;
self.mainTableView.emptyDataSetDelegate = self;
- 實(shí)現(xiàn)代理方法
- DZNEmptyDataSetDelegate
/**
* 數(shù)據(jù)源為空渲染和顯示(默認(rèn)是YES)
*/
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
return YES;
}
/**
* 是否允許點(diǎn)擊(默認(rèn)是YES)
*/
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
return YES;
}
/**
* 是否允許滾動(默認(rèn)是NO)
*/
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
return NO;
}
/**
* 空白區(qū)域點(diǎn)擊事件
*/
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
}
/**
* 點(diǎn)擊按鈕事件處理
*/
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
NSLog(@"小樣 不要點(diǎn)我了啊");
}
- DZNEmptyDataSetSource
/**
* 返回標(biāo)題文字
*/
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"這是標(biāo)題";
return [[NSAttributedString alloc]initWithString:text];
}
/**
* 返回文字詳情
*/
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"這只一段短描述";
NSMutableAttributedString *attribuString = [[NSMutableAttributedString alloc]initWithString:text attributes:nil];
[attribuString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:[attribuString.string rangeOfString:@"哈哈哈"]];
return attribuString;
}
/**
* 返回文字按鈕
*/
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
NSString *text = @"點(diǎn)我重新加載";
UIFont *font = [UIFont systemFontOfSize:15];
UIColor *color = [UIColor redColor];
NSMutableDictionary *attributedString = [[NSMutableDictionary alloc]init];
[attributedString setObject:font forKey:NSFontAttributeName];
[attributedString setObject:color forKey:NSForegroundColorAttributeName];
return [[NSAttributedString alloc]initWithString:text attributes:attributedString];
}
/**
* 返回圖片按鈕
*/
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
return [UIImage imageNamed:@"nodata"];
}
/**
* 調(diào)整垂直位置
*/
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
return -64.f;
}
/**
* 圖片動畫效果
*/
- (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;
}

最后來一波無恥的廣告:淘劵吧