DZNEmptyDataSet 在Swift4.1 中使用

DZNEmptyDataSet github地址

Swift4.1 使用 DZNEmptyDataSet在最下面↓↓↓↓

只要通過遵循 DZNEmptyDataSetSourceDZNEmptyDataSetDelegate 協議,您將能夠完全自定義應用程序的空狀態的內容和外觀。當 UITableViewUICollectionView沒有要顯示的內容時,它用于顯示空數據集界面。

這兩個協議中的協議方法均為 @optional 類型。

特點

  • 兼容 UITableViewUICollectionView 也兼容 UISearchDisplayControllerUIScrollView
  • 通過 顯示圖像 / 標題標簽 / 描述標簽 / 按鈕,給出布局和外觀的多種可能性。
  • 使用 NSAttributedString 得到更容易的外觀定制。
  • 使用 Auto Layout 以自動將內容集中到表格視圖,并支持自動旋轉。也接受自定義垂直和水平對齊
  • 背景顏色可定制。
  • 允許在整個表格矩形上輕敲手勢
  • 對于更高級的自定義,它允許自定義視圖
  • 兼容 Storyboard

reloadEmptyDataSet 是使用 UIScrollView 時刷新內容的 唯一方法

效果圖


使用方法

可以使用 CocoaPods 導入

可以使用 Cartfile 導入

也可以直接將 UIScrollView+EmptyDataSet.h UIScrollView+EmptyDataSet.m 直接拖到項目中

Swift 需要在你的 ObjC_Bridging_Header_h 中添加

#import "UIScrollView+EmptyDataSet.h"

就可以開心的使用了

DZNEmptyDataSet協議

- (void)reloadEmptyDataSet; 

調用 UITableView 或者 UICollectionViewreloadData 方法便會相應此方法。并且 當且僅當 列表數據源為空的時候才會觸發。

DZNEmptyDataSetSource

該協議主要作用于數據源為空時的對空白界面元素的設置。

其中包括對 title description image imageTintColor imageAnimation buttonTitle buttonImage等屬性的設置。

該協議提供了一套配置的接口,既方便用戶根據需求設置相應的樣式,當然也提供了自定義界面的接口

設置默認空白界面處理視圖的標題title.

若需要設置富文本,則返回時設置 (NSAttributedString *) 類型。

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView; 

設置默認空白界面處理視圖的描述description文本。

若需要設置富文本,則返回時設置 (NSAttributedString *) 類型。

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView; 

設置默認空白界面布局的圖片。

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView; 

設置默認空白界面布局圖片的前景色,默認為nil.

- (UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView; 

設置默認空白界面圖片的動畫效果。

- (CAAnimation *) imageAnimationForEmptyDataSet:(UIScrollView *) scrollView; 

設置默認空白界面響應按鈕的標題,通常我們可以設置為"重新加載"等文本。

如果需要顯示不同的標題樣式,可以返回富文本。
并傳入 UIControlState 進行設置。點擊或者普通狀態等。

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

設置默認空白界面響應按鈕的圖片。

并傳入 UIControlState 進行設置。點擊或者普通狀態等。

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

設置默認空白界面響應按鈕的背景圖片。默認不設置。

并傳入 UIControlState 進行設置。點擊或者普通狀態等。

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

設置默認空白界面的背景顏色。默認為 [UIColor clearColor]

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView; 

設置默認空白界面的自定義視圖View, View 中可以高度自定義,包括按鈕圖片以及標題等元素。

并傳入 UIControlState 進行設置。點擊或者普通狀態等。
返回自定義視圖,將會忽略以下方法的配置。-offsetForEmptyDataSet-spaceHeightForEmptyDataSet

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView; 

設置界面的垂直和水平方向的對齊約束, 默認為CGPointZero

- (CGPoint)offsetForEmptyDataSet:(UIScrollView *)scrollView DEPRECATED_MSG_ATTRIBUTE("Use -verticalOffsetForEmptyDataSet:"); 
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView; 

設置界面元素的垂直間距,默認為11px

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

DZNEmptyDataSetDelegate

該協議主要作用于處理該空白界面的代理。用于獲取代理的響應回調。

實現該方法告訴代理 EmptyDataSetView 顯示時以淡入的模式,默認為YES

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

實現該方法告訴代理 EmptyDataSetView 顯示時應該被渲染。默認為YES

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

實現該方法告訴代理該視圖可以響應點擊事件,默認為YES

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

實現該方法告訴代理該視圖允許滾動,默認為NO

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

實現該方法告訴代理該視圖中的圖片允許執行動畫,默認為NO

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

實現該方法告訴代理 emptyDataSetView 被點擊

使用該方法要么對textfield或者searchBar調用了resignFirstResponder方法。

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView DEPRECATED_MSG_ATTRIBUTE("Use emptyDataSet:didTapView:");

實現該方法告訴代理,響應按鈕點擊事件被觸發

scrollView 該滾動視圖的子類實現了該方法。

- (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView DEPRECATED_MSG_ATTRIBUTE("Use emptyDataSet:didTapButton:"); 

實現該方法告訴代理empty dataset view被點擊觸發。

使用該方法要么對textfield或者searchBar調用了resignFirstResponder方法。

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

實現該方法告訴代理,響應按鈕點擊事件被觸發

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

實現該方法告訴代理,emptyDataView視圖即將出現。

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

實現該方法告訴代理,emptyDataView視圖已經出現。

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

實現該方法告訴代理,emptyDataView視圖即將消失。

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

實現該方法告訴代理,emptyDataView視圖已經消失。

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

在Swift4.1中使用

遵循協議

如果你使用storyboard 或者 xib 直接如下設置

如果使用純代碼

TableView為例

lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        return tableView
    }()

實現協議

為了減少入侵業務代碼我將 DZNEmptyDataSetSource DZNEmptyDataSetDelegate 統一寫在 EmptyDataExtension.swift 里面

基于項目需求,統一配置為 UIViewController 的擴展

//
//
// EmptyDataExtension.swift
//
//

// MARK: - GlobalSetting
extension UIViewController: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
    public func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return UIImage.init(named: "null_def")
    }
    
    public func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return -80
    }
    
    public func spaceHeight(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return 10
    }
    
    public func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
        return UIColor.white
    }
    
    public func emptyDataSetShouldDisplay(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    
    public func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    
    public func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
        return false
    }
    
    public func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView) -> Bool {
        return false
    }
}

針對不同業務邏輯做不同配置

// MARK: - 視圖1
extension ViewControllerOne {
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let text = "視圖1 Title"
        let attributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: CGFloat(16.0)),
                          NSAttributedStringKey.foregroundColor: UIColor.black]
        return NSAttributedString(string: text, attributes: attributes as [NSAttributedStringKey : Any])
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let text = "視圖1 Description"
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineBreakMode = .byWordWrapping
        paragraph.alignment = .center
        let attributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: CGFloat(13.0)),
                          NSAttributedStringKey.foregroundColor: UIColor.black,
                          NSAttributedStringKey.paragraphStyle:paragraph]
        return NSAttributedString(string: text, attributes: attributes as [NSAttributedStringKey : Any])
    }
    
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControlState) -> NSAttributedString? {
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        return NSAttributedString(string: "視圖1 Button", attributes: attributes as [NSAttributedStringKey : Any])
    }
    
    func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControlState) -> UIImage? {
        switch state {
        case .highlighted:
            return UIImage.init(named: "1")
        default:
            return UIImage.init(named: "2")
        }
    }
    
    func emptyDataSet(_ scrollView: UIScrollView, didTap button: UIButton) {
        /// do some thing
    }
}
// MARK: - 視圖2
extension ViewControllerTwo {
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let text = "視圖2 Title"
        let attributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: CGFloat(16.0)),
                          NSAttributedStringKey.foregroundColor: UIColor.black]
        return NSAttributedString(string: text, attributes: attributes as [NSAttributedStringKey : Any])
    }
}
// MARK: - 視圖3
extension ViewControllerThree {
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let text = "視圖3 Title"
        let attributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: CGFloat(16.0)),
                          NSAttributedStringKey.foregroundColor: UIColor.black]
        return NSAttributedString(string: text, attributes: attributes as [NSAttributedStringKey : Any])
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let text = "視圖3 Description"
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineBreakMode = .byWordWrapping
        paragraph.alignment = .center
        let attributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: CGFloat(13.0)),
                          NSAttributedStringKey.foregroundColor: UIColor.black,
                          NSAttributedStringKey.paragraphStyle:paragraph]
        return NSAttributedString(string: text, attributes: attributes as [NSAttributedStringKey : Any])
    }
}


修改某一個頁面的全局屬性

// MARK: - 視圖2
extension ViewControllerTwo {
    override func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return UIImage.init(named: "null_two")
    }
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let text = "視圖2 Title"
        let attributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: CGFloat(16.0)),
                          NSAttributedStringKey.foregroundColor: UIColor.black]
        return NSAttributedString(string: text, attributes: attributes as [NSAttributedStringKey : Any])
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,663評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,125評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,506評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,614評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,402評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,934評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,021評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,168評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,690評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,596評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,288評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,027評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,404評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,662評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,398評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,743評論 2 370

推薦閱讀更多精彩內容