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])
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容