EmptyKit
一个轻盈的 Swift 库,在视图(tableView/collectionView)没有内容可显示时显示 emptyView,就像 DZNEmptyDataSet
要求
- iOS 8.0+
- Xcode 8.0+
- Swift4 (EmptyKit 4.x) Swift3(EmptyKit 3.x)
安装
Carthage
创建一个包含框架的 Cartfile
。根据 说明 将 $(SRCROOT)/Carthage/Build/iOS/EmptyKit.framework
添加到 iOS 项目中。
github "ZionChang/EmptyKit"
运行 carthage update
构建框架,并将其构建的 EmptyKit.framework
拖放到您的 Xcode 项目中。
要获得全部利益,请导入 EmptyKit
import EmptyKit
CocoaPods
您可以使用 CocoaPods,通过将其添加到您的 Podfile
中来安装 EmptyKit
platform :ios, '8.0'
use_frameworks!
pod 'EmptyKit'
然后,运行以下命令
$ pod install
手动操作
- 下载并将
Empty
拖入您的项目中。 - 恭喜您!
使用方法
遵守协议
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, 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()
或者
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 {}
要求
- 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
手工
- 下载并将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: [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 {}
版权声明
EmptyKit 遵循 MIT 许可协议,请参阅 LICENSE 文件。