简介
EmptyDataSet-Swift和DZNEmptyDataSet,用于在 UITableView 和 UICollectionView 中显示空数据集。
在OC开发中我们可以用DZNEmptyDataSet快速实现在空数据条件下显示view。
EmptyDataSet-Swift它是对 DZNEmptyDataSet 的 Swift版本,使用和方法类似。
应用场景:创建多种场景的空态视图
1 用户首次使用,数据尚未加载:展示引导信息,鼓励用户进行首次操作。
2 搜索结果为空:提示用户没有找到相关结果,可以提供二次搜索建议。
3 网络错误:显示重试按钮,方便用户立即尝试重新加载。
EmptyDataSet-Swift具体使用方法一:
1 遵循 EmptyDataSetSource 和 EmptyDataSetDelegate 协议
自定义UITableView 或 UICollectionView 的代理类遵循协议,也可以自定义一个ViewController包含的UITableView或者UICollectionView遵循协议。
这里我倾向于自定义base视图控件。
tableView.emptyDataSetSource = self
tableView.emptyDataSetDelegate = self
2 实现代理
下列代码实现了,标签,按钮,image,动画等功能。他们之间垂直居中分布。
emptyDataSetShouldDisplay默认numberOfRowsInSection 返回 0时返回true
设置 customView,其他设置无效。使用 Extensions 设置 customView,其他 autolayout 将无效。
框架提供了很多其它方法比如:
emptyDataSetWillAppear,emptyDataSetDidDisappear等页面周期方法。
extension BaseTableView: EmptyDataSetSource {
func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
let text = titleEmptyNoData
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.darkGray]
return NSAttributedString(string: text, attributes: attributes)
}
func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
let text = descriptionEmptyNoData
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = .center
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18), NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.paragraphStyle: paragraphStyle]
return NSAttributedString(string: text, attributes: attributes)
}
func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
return NSAttributedString(string: "重新加载")
}
func buttonImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> UIImage? {
return UIImage(named: "RDEmptyData.bundle/NullData_reloadData")
}
func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
return 10
}
func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
return UIImage(named: "Alert_error")
}
func imageAnimation(forEmptyDataSet scrollView: UIScrollView) -> CAAnimation? {
let animation = CABasicAnimation(keyPath: "transform.rotation.z")
animation.fromValue = 0
animation.toValue = Double.pi * 2
animation.duration = 5
animation.autoreverses = false
animation.fillMode = .forwards
animation.repeatCount = MAXFLOAT
animation.isRemovedOnCompletion = false
return animation
}
/**
/// 如果设置了customView,其它控件设置无效
func customView(forEmptyDataSet scrollView: UIScrollView) -> UIView? {
let view = CustomView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
return view
}
*/
}
extension BaseTableView: EmptyDataSetDelegate {
func emptyDataSetShouldDisplay(_ scrollView: UIScrollView) -> Bool {
return numberOfRows(inSection: 0) == 0
}
func emptyDataSet(_ scrollView: UIScrollView, didTapView view: UIView) {
print("点击了UIScrollView")
}
func emptyDataSet(_ scrollView: UIScrollView, didTapButton button: UIButton) {
print("点击了按钮")
}
func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView) -> Bool {
return true
}
}
做点一适配场景:
在没有业务数据时: mTableView.setUpEmptyDataSet(.StateNoData)
没有网络时: mTableView.setUpEmptyDataSet(.StateNetWorkError)
enum EmptyDataState: Int {
/// 状态0 - 暂无数据
case StateNoData = 0
/// 状态1 -网络请求错误,(网络不可用,请检查网络设置)
case StateNetWorkError
/// 状态2 - other
case StateOhter
}
private let kNoDataStr = "暂无数据"
private let kNetWorkErrorStr = "网络不给力,点击重新加载"
var titleEmptyNoData:String = ""
var descriptionEmptyNoData:String = ""
func setUpEmptyDataSet(_ type:EmptyDataState) {
switch type {
case .StateNoData:
titleEmptyNoData = kNoDataStr
descriptionEmptyNoData = "努力为您查找..."
case .StateNetWorkError:
titleEmptyNoData = kNoDataStr
descriptionEmptyNoData = "网络不给力,点击重新加载"
case .StateOhter:
titleEmptyNoData = ""
descriptionEmptyNoData = ""
}
self.initMethod()
/// self.reloadEmptyDataSet()
}
private func initMethod() {
backgroundColor = .white
configIOS11()
emptyDataSetSource = self
emptyDataSetDelegate = self
}
EmptyDataSet-Swift具体使用方法二:
emptyDataSetView我们直接可以设置属性方法,不需要设置代理。
同时这些lazy属性支持链式调用。
private func initMethod() {
emptyDataSetView { view in
view.titleLabelString(kNoDataStr)
.detailLabelString(kNetWorkErrorStr)
.image(UIImage(named: "Alert_error"))
.shouldDisplay(true)
.didTapDataButton {
// Do something
mlog("点击重新加载按钮")
}
.didTapContentView {
// Do something
mlog("点击ContentView")
}
}
}
DZNEmptyDataSet方法
因为EmptyDataSet-Swift是DZNEmptyDataSet的swift版本,使用方法一样。
遵循 DZNEmptyDataSetSource 和 DZNEmptyDataSetDelegate 协议,实现协议。