EmptyKit Save

A lightweight, swift library for displaying emptyView whenever the view(tableView/collectionView) has no content to display, just like DZNEmptyDataSet

Project README

EmptyKit

A lightweight, swift library for displaying emptyView whenever the view(tableView/collectionView) has no content to display, just like DZNEmptyDataSet

Swift Version Pod Version Carthage compatible License Platform

Requirements

  • iOS 8.0+
  • Xcode 8.0+
  • Swift4 (EmptyKit 4.x) Swift3(EmptyKit 3.x)

Installation

Carthage

Create a Cartfile that lists the framework. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/EmptyKit.framework to an iOS project.

github "ZionChang/EmptyKit"

Run carthage update to build the framework and drag the built EmptyKit.framework into your Xcode project.

To get the full benefits import EmptyKit

import EmptyKit

CocoaPods

You can use CocoaPods to install EmptyKit by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!
pod 'EmptyKit'

Then, run the following command:

$ pod install

Manually

  1. Download and drop Empty in your project.
  2. Congratulations!

Usage

Protocol Conformance

final class DetailTableViewController: UITableViewController { 

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        tableView.ept.dataSource = self
        tableView.ept.delegate = self
    }

}

Data Source Imp

extension DetailTableViewController: EmptyDataSource {

    func imageForEmpty(in view: UIView) -> UIImage? {
        return UIImage(named: "empty_data_bookshelf")
    }

    func titleForEmpty(in view: UIView) -> NSAttributedString? {
        let title = "no data"
        let font = UIFont.systemFont(ofSize: 14)
        let attributes: [NSAttributedStringKey : Any] = [.foregroundColor: UIColor.black, .font: font]
        return NSAttributedString(string: title, attributes: attributes)
    }

    func buttonTitleForEmpty(forState state: UIControlState, in view: UIView) -> NSAttributedString? {
        let title = "click me"
        let font = UIFont.systemFont(ofSize: 17)
        let attributes: [NSAttributedStringKey : Any] = [.foregroundColor: UIColor.white, .font: font]
        return NSAttributedString(string: title, attributes: attributes)
    }

    func buttonBackgroundColorForEmpty(in view: UIView) -> UIColor {
        return UIColor.blue
    }

}

Or you can implement other methods of EmptyDataSource

Delegate Imp

extension DetailTableViewController: EmptyDelegate {

    func emptyButton(_ button: UIButton, didTappedIn view: UIView) {
        print( #function, #line, type(of: self))
    }

    func emptyView(_ emptyView: UIView, didTapppedIn view: UIView) {
        print( #function, #line, type(of: self))
    }
}

Refresh layout

self.tableView.reloadData()

or

self.collectionView.reloadData()

depending of which you are using.

Force layout update

self.tableView.ept.reloadData()

or

self.collectionView.ept.reloadData()

Global Configuration

  1. Conform EmptyDataSource or EmptyDelegate
protocol ProjectNameEmptyDataSource: EmptyDataSource {}
extension ProjectNameEmptyDataSource {
    // implement any method you want
    func backgroundColorForEmpty(in view: UIView) -> UIColor {
        return UIColor.white
    }
    // other methods
}

protocol ProjectNameEmptyDelegate: EmptyDelegate {}
extension ProjectNameEmptyDelegate {
    // just like the ProjectNameEmptyDataSource
}

final class ProjectNameViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        tableView.ept.dataSource = self
        tableView.ept.delegate = self
    }
}
  1. Remember use your custom protocol
extension ProjectNameViewController: ProjectNameEmptyDataSource {}
extension ProjectNameViewController: ProjectNameEmptyDelegate {}

# 中文介绍

要求

  • iOS8.0+
  • Xcode 8.0+

安装

Cathage

首先需要安装Carthage

创建一个Cartfile,在其中列出需要的framework

github "ZionChang/EmptyKit"	

命令行运行carthage update来构建framework,并且将EmptyKit.framework拖拽到Xcode中。

import EmptyKit

CocoaPods

创建Podfile

platform :ios, '8.0'
use_frameworks!
pod 'EmptyKit', '~> 3.0.2'

然后运行

pod install

手动

  1. 下载并且将EmptyKit文件夹拖拽至你的工程
  2. 恭喜!

用法

遵守协议

final class DetailTableViewController: UITableViewController { 

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        tableView.ept.dataSource = self
        tableView.ept.delegate = self
    }

}

数据源实现

extension DetailTableViewController: EmptyDataSource {

    func imageForEmpty(in view: UIView) -> UIImage? {
        return UIImage(named: "empty_data_bookshelf")
    }

    func titleForEmpty(in view: UIView) -> NSAttributedString? {
        let title = "no data"
        let font = UIFont.systemFont(ofSize: 14)
        let attributes: [NSAttributedStringKey : Any] = [.foregroundColor: UIColor.black, .font: font]
        return NSAttributedString(string: title, attributes: attributes)
    }

    func buttonTitleForEmpty(forState state: UIControlState, in view: UIView) -> NSAttributedString? {
        let title = "click me"
        let font = UIFont.systemFont(ofSize: 17)
        let attributes: [NSAttributedStringKey : Any] = [.foregroundColor: UIColor.white, .font: font]
        return NSAttributedString(string: title, attributes: attributes)
    }

    func buttonBackgroundColorForEmpty(in view: UIView) -> UIColor {
        return UIColor.blue
    }

}

或者你能实现EmptyDataSource中的其他方法

代理实现

extension DetailTableViewController: EmptyDelegate {

    func emptyButton(_ button: UIButton, tappedIn view: UIView) {
        print( #function, #line, type(of: self))
    }

    func emptyView(_ emptyView: UIView, tappedIn view: UIView) {
        print( #function, #line, type(of: self))
    }
}

刷新布局

self.tableView.reloadData()

或者

self.collectionView.reloadData()

强制刷新空视图

self.tableView.ept.reloadData()

或者

self.collectionView.ept.reloadData()

全局配置

  • 遵守EmptyDataSource或者EmptyDelegate
protocol ProjectNameEmptyDataSource: EmptyDataSource {}
extension ProjectNameEmptyDataSource {
    // implement any method you want
    func backgroundColorForEmpty(in view: UIView) -> UIColor {
        return UIColor.white
    }
    // other methods
}

protocol ProjectNameEmptyDelegate: EmptyDelegate {}
extension ProjectNameEmptyDelegate {
    // just like the ProjectNameEmptyDataSource
}

final class ProjectNameViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        tableView.ept.dataSource = self
        tableView.ept.delegate = self
    }
}
  • 一旦全局配置,就使用你配置的协议名字
extension ProjectNameViewController: ProjectNameEmptyDataSource {}
extension ProjectNameViewController: ProjectNameEmptyDelegate {}

License

EmptyKit is licensed under the MIT License, please see the LICENSE file.

Open Source Agenda is not affiliated with "EmptyKit" Project. README Source: ZionChang/EmptyKit
Stars
117
Open Issues
3
Last Commit
5 years ago
Repository
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating