首先,在应用很多时候都要用到暂无数据的视图。
这里用一个协议的方式,让需要显示的视图拥有该功能。
1.首先定义一个协议:
protocol LsqNotData {
var showView:UIView? {get}
}
这里的 showView 是需要展示无数据视图的View
2.定义一个 显示的图片枚举,这里你可以写很多种哈
enum LsqNotDataType {
case normal
case notData
case search
func getImgAndRect() -> (String,CGSize) {
var imgName =""
var size =CGSize()
switch self{
case.normal:
imgName ="无数据01"
size =CGSize(width:148.0 / 2 * S.scale375, height:171.0 / 2 * S.scale375)
case.notData:
imgName ="数据为空"
size =CGSize(width:240.0 / 2 * S.scale375, height:276.0 / 2 * S.scale375)
case.search:
imgName ="icon__saearch"
size =CGSize(width:189.0 / 2 * S.scale375, height:185.0 / 2 * S.scale375)
}
return(imgName,size)
}
}
说明:这里的 size 为实际需要显示图片大小,S.scale375为本人定义用于适配设计图的一个适配比例
struct S {
///屏幕宽度
static let width = UIScreen.main.bounds.width
///屏幕高度
static let height = UIScreen.main.bounds.height
///宽度比例,按照375宽度
static var scale375: CGFloat {
return self.width /375.0
}
}
3.给 LsqNotData 延展几个方法(已实现)
extension LsqNotData {
///LsqNotDataType 显示的类型,说白了就是选择哪个图片
///point为显示的中心点,如果为空,则默认为 showView 的中心点
///text在图片下放的文字
///offset 图片的偏移位置,为nil则不偏移。x,y的值如果<0,则向左和上偏移
func showNotData(with type: LsqNotDataType, point: CGPoint?, text: String?, offset: CGSize?) {
self.removeNotDataView()
if self.showView ==nil {
return
}
let(imgName,size) = type.getImgAndRect()
var frame =CGRect()
if let p = point{
frame =CGRect(x: (self.showView!.frame.width-size.width)/2, y: p.y-size.height/2, width: size.width, height: size.height)
}else{
frame =CGRect(x: (self.showView!.frame.width-size.width)/2, y: (self.showView!.frame.height-size.height)/2, width: size.width, height: size.height)
}
let imgView =UIImageView(frame: frame)
imgView.image=UIImage(named: imgName)
imgView.tag=6667771
self.showView?.addSubview(imgView)
if let tmpOffset = offset {
imgView.top+=tmpOffset.height
imgView.left+=tmpOffset.width
}
let label =UILabel(frame:CGRect(x:0, y: imgView.bottom+10, width:self.showView!.frame.width, height:20*S.scale375))
label.text= text
label.numberOfLines=0
label.font=UIFont.auto(font:13)
label.textAlignment = .center
label.textColor=UIColor.hexColor(with:"#5e7181")
label.tag=6667772
label.sizeToFit()
label.center.x=self.showView!.frame.width/2
self.showView?.addSubview(label)
}
///调用该方法移除无数据视图
func removeNotDataView(){
letimgView1 =self.showView?.viewWithTag(6667771)
imgView1?.removeFromSuperview()
letlabel1 =self.showView?.viewWithTag(6667772)
label1?.removeFromSuperview()
let other =self.showView?.viewWithTag(6667773)
other?.removeFromSuperview()
}
///添加一个其他视图,可以点击的
func addOtherNotDataView(_view:UIView?, labelBottomY:CGFloat, target:Any?, action:Selector?) {
guard let vv = view else{return}
if let act = action {
view?.isUserInteractionEnabled = true
let tap =UITapGestureRecognizer(target: target, action: act)
vv.addGestureRecognizer(tap)
}
if let label =self.showView?.viewWithTag(6667772) {
vv.top= label.bottom+labelBottomY
vv.center.x= label.center.x
vv.tag=6667773
self.showView?.addSubview(vv)
}
}
}
例如一个TableView
class MessgeTableView: UITableView,LsqNotData {
var showView:UIView? {
return self
}
public var dadaArry = [Model](){
didSet{
if self.dadaArry.isEmpty{
self.showNotData(with: .notData, point:nil, text:"暂无数据", offset:nil)
}else{
self.removeNotDataView()
}
self.reloadData()
}
}
}
例如一个Controller
class AddressBookSearchController: BaseViewController, LsqNotData {
public var dataArray = [AddressBookModel]() {
didSet{
if self.dataArray.isEmpty{
self.showNotData(with: .notData, point:nil, text:"暂无数据", offset:nil)
}else{
self.removeNotDataView()
}
self.myTabView?.reloadData()
}
}
private var myTabView:UITableView?
var showView:UIView? {
return self.myTabView
}
}
只需要实现 LsqNotData 协议 就可以拥有显示无数据的能力啦。
要不要试试呢?