A lightweight, swift library for displaying emptyView whenever the view(tableView/collectionView) has no content to display, just like DZNEmptyDataSet
- iOS 8.0+
- Xcode 8.0+
###Carthage
Create a Cartfile
that lists the framework. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/EmptyKit.framework
to an iOS project.
github "eilianlove/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', '~> 3.1.0'
Then, run the following command:
$ pod install
- Download and drop
Empty
in your project. - Congratulations!
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: [String : Any] = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: 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: [String : Any] = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: font]
return NSAttributedString(string: title, attributes: attributes)
}
func buttonBackgroundColorForEmpty(in view: UIView) -> UIColor {
return UIColor.blue
}
}
Or you can implement other methods of EmptyDataSource
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))
}
}
self.tableView.reloadData()
or
self.collectionView.reloadData()
depending of which you are using.
self.tableView.ept.reloadData()
or
self.collectionView.ept.reloadData()
- 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
}
}
- Remember use your custom protocol
extension ProjectNameViewController: ProjectNameEmptyDataSource {}
extension ProjectNameViewController: ProjectNameEmptyDelegate {}
- iOS8.0+
- Xcode 8.0+
首先需要安装Carthage
创建一个Cartfile
,在其中列出需要的framework
github "eilianlove/EmptyKit"
命令行运行carthage update
来构建framework,并且将EmptyKit.framework
拖拽到Xcode中。
import EmptyKit
创建Podfile
platform :ios, '8.0'
use_frameworks!
pod 'EmptyKit', '~> 3.0.2'
然后运行
pod install
- 下载并且将EmptyKit文件夹拖拽至你的工程
- 恭喜!
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: [String : Any] = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: 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: [String : Any] = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: 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 {}