DZNEmptyDataSet詳解

Demo傳送門

摘要: 主要用于UITableView和UICollectionView,UIScrollView,數(shù)據(jù)為空時,頁面一片空白,不是一個很好的用戶體驗(yàn)。我們都希望在數(shù)據(jù)源為空的時候,給用戶一些相應(yīng)的提示,DZNEmptyDataSet完美解決這個問題。

使用詳解

  1. 導(dǎo)入第三方,遵守協(xié)議
#import "UIScrollView+EmptyDataSet.h"
遵守 <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate> 協(xié)議
  1. 設(shè)置代理
 self.mainTableView.emptyDataSetSource = self;
 self.mainTableView.emptyDataSetDelegate = self;
  1. 實(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;
    }


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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容