DZNEmptyDataSet 在Swift4.1 中使用

DZNEmptyDataSet github地址

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

只要通过遵循 DZNEmptyDataSetSourceDZNEmptyDataSetDelegate 协议,您将能够完全自定义应用程序的空状态的内容和外观。当 UITableViewUICollectionView没有要显示的内容时,它用于显示空数据集界面。

这两个协议中的协议方法均为 @optional 类型。

特点

  • 兼容 UITableViewUICollectionView 也兼容 UISearchDisplayControllerUIScrollView
  • 通过 显示图像 / 标题标签 / 描述标签 / 按钮,给出布局和外观的多种可能性。
  • 使用 NSAttributedString 得到更容易的外观定制。
  • 使用 Auto Layout 以自动将内容集中到表格视图,并支持自动旋转。也接受自定义垂直和水平对齐
  • 背景颜色可定制。
  • 允许在整个表格矩形上轻敲手势
  • 对于更高级的自定义,它允许自定义视图
  • 兼容 Storyboard

reloadEmptyDataSet 是使用 UIScrollView 时刷新内容的 唯一方法

效果图

DZNEmptyDataSet
DZNEmptyDataSet

使用方法

可以使用 CocoaPods 导入

可以使用 Cartfile 导入

也可以直接将 UIScrollView+EmptyDataSet.h UIScrollView+EmptyDataSet.m 直接拖到项目中

然后在你的 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 直接如下设置

xib.png

如果使用纯代码

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

猜你喜欢

转载自blog.csdn.net/jichunw/article/details/80089067