[swift]使用DZNEmptyDataSet空数据占位图

在swift开发中,会遇到页面无数据的情况,当没有数据时,页面一片空白,顿时令用户心情很不好,在OC中自己定义的占位图View不免有点太low,于是采用第三方占位图框架DZNEmptyDataSet,如今在swift中也需要使用此此景,下面就介绍一下如何在swift中使用DZNEmptyDataSet。

OC版本的 DZNEmptyDataSet gitHub传送门 :DZNEmptyDataSet

DZNEmptyDataSet swift 使用方法的传送门DZNEmptyDataSetSwift

1.新建swift工程,并导入第三方

$ pod init

在Podfile中添加  DZNEmptyDataSet三方框架
use_frameworks!
#占位图
pod 'DZNEmptyDataSet'

然后pod install
新建swift工程,并导入第三方

2.创建一个OC转swift的桥接对象文件

24B06FC5-2FC0-4A46-A8A0-568E7010C2C4.png
72CEB3CC-FF08-43DB-B56E-71AD8B38FC8C.png

3.新建一个viewController,添加tableView 设置emptyData代理

 fileprivate lazy var tableView: UITableView = {[weak self] in
        let tableView = UITableView()
        tableView.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1.0)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.separatorStyle = .none
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        tableView.delegate = self;
        tableView.dataSource = self;
        return tableView
        }()

4.实现代理协议

extension ViewController: DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellCount
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    
    //MARK: -- DZNEmptyDataSetSource Methods
    //标题为空的数据集
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = "没有数据";
        let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(18.0)), NSForegroundColorAttributeName: UIColor.blue]
        return NSAttributedString(string: text, attributes: attributes)
    }
    
    //描述为空的数据集
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let paragraph = NSMutableParagraphStyle()
        paragraph.alignment = .center
        paragraph.lineSpacing = CGFloat(NSLineBreakMode.byWordWrapping.rawValue)
        let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(15.0)), NSForegroundColorAttributeName: UIColor.red, NSParagraphStyleAttributeName: paragraph]
        return NSAttributedString(string: "真的没有数据!", attributes: attributes)
    }
    ////空数据按钮图片
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        if isLoading! { //加载动画图片
            return UIImage(named: "youku_refreshing")
        } else {
            return UIImage(named: "placeholder_instagram")
        }
    }
    
    //数据集加载动画
    func imageAnimation(forEmptyDataSet scrollView: UIScrollView!) -> CAAnimation! {
        let animation = CABasicAnimation(keyPath: "transform")
        animation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
        animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(CGFloat(Double.pi / 2), 0.0, 0.0, 1.0))
        animation.duration = 0.25
        animation.isCumulative = true
        animation.repeatCount = MAXFLOAT
        return animation as CAAnimation
    }
    //按钮标题为空的数据集
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControlState) -> NSAttributedString! {
        let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(16.0)), NSForegroundColorAttributeName: (state == UIControlState.normal) ? UIColor.brown : UIColor.green]
        return NSAttributedString(string: "重新加载", attributes: attributes)
    }
    
    ////重新加载按钮背景图片
    func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView!, for state: UIControlState) -> UIImage! {
        let image = UIImage(named: state == UIControlState.normal ? "button_background_foursquare_normal" : "button_background_foursquare_highlight")
        return image?.resizableImage(withCapInsets: UIEdgeInsetsMake(25.0, 25.0, 25.0, 25.0), resizingMode: .stretch).withAlignmentRectInsets(UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0))
        
    }
    
    func backgroundColor(forEmptyDataSet scrollView: UIScrollView!) -> UIColor! {
        return UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1.0)
    }
    
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return 0
    }
    
    func spaceHeight(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return 10
    }
    
    //MARK: -- DZNEmptyDataSetDelegate
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView!) -> Bool {
        return self.isLoading!
    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap view: UIView!) {
        isLoading = true
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.isLoading = false
            self.reloadTableView()


        }
    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap button: UIButton!) {
        self.isLoading = true
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.isLoading = false
            self.cellCount = 10
            self.reloadTableView()
            
        }
    }
    
    fileprivate func reloadTableView(){
        self.tableView.reloadData()
        self.tableView.reloadEmptyDataSet()
    }
    
}

5.附上完整代码

extension ViewController: DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellCount
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    
    //MARK: -- DZNEmptyDataSetSource Methods
    //标题为空的数据集
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = "没有数据";
        let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(18.0)), NSForegroundColorAttributeName: UIColor.blue]
        return NSAttributedString(string: text, attributes: attributes)
    }
    
    //描述为空的数据集
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let paragraph = NSMutableParagraphStyle()
        paragraph.alignment = .center
        paragraph.lineSpacing = CGFloat(NSLineBreakMode.byWordWrapping.rawValue)
        let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(15.0)), NSForegroundColorAttributeName: UIColor.red, NSParagraphStyleAttributeName: paragraph]
        return NSAttributedString(string: "真的没有数据!", attributes: attributes)
    }
    ////空数据按钮图片
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        if isLoading! { //加载动画图片
            return UIImage(named: "youku_refreshing")
        } else {
            return UIImage(named: "placeholder_instagram")
        }
    }
    
    //数据集加载动画
    func imageAnimation(forEmptyDataSet scrollView: UIScrollView!) -> CAAnimation! {
        let animation = CABasicAnimation(keyPath: "transform")
        animation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
        animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(CGFloat(Double.pi / 2), 0.0, 0.0, 1.0))
        animation.duration = 0.25
        animation.isCumulative = true
        animation.repeatCount = MAXFLOAT
        return animation as CAAnimation
    }
    //按钮标题为空的数据集
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControlState) -> NSAttributedString! {
        let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(16.0)), NSForegroundColorAttributeName: (state == UIControlState.normal) ? UIColor.brown : UIColor.green]
        return NSAttributedString(string: "重新加载", attributes: attributes)
    }
    
    ////重新加载按钮背景图片
    func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView!, for state: UIControlState) -> UIImage! {
        let image = UIImage(named: state == UIControlState.normal ? "button_background_foursquare_normal" : "button_background_foursquare_highlight")
        return image?.resizableImage(withCapInsets: UIEdgeInsetsMake(25.0, 25.0, 25.0, 25.0), resizingMode: .stretch).withAlignmentRectInsets(UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0))
        
    }
    
    func backgroundColor(forEmptyDataSet scrollView: UIScrollView!) -> UIColor! {
        return UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1.0)
    }
    
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return 0
    }
    
    func spaceHeight(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return 10
    }
    
    //MARK: -- DZNEmptyDataSetDelegate
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView!) -> Bool {
        return self.isLoading!
    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap view: UIView!) {
        isLoading = true
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.isLoading = false
            self.reloadTableView()


        }
    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap button: UIButton!) {
        self.isLoading = true
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.isLoading = false
            self.cellCount = 10
            self.reloadTableView()
            
        }
    }
    
    fileprivate func reloadTableView(){
        self.tableView.reloadData()
        self.tableView.reloadEmptyDataSet()
    }
    
}

推荐阅读更多精彩内容