YSNoDataView 1.0.9

YSNoDataView 1.0.9

Maintained by ys.



  • By
  • ys

YSNoDataView

UITableView、UICollectionView无数据时空白占位视图

环境

swift4.2、iOS9.0

使用步骤

1、导入框架

pod 'YSNoDataView'

2、导入命名空间

import YSNoDataView

以下以UITableView为例

UITableView与UICollectionView使用方法类似。

UITableView使用步骤

1、自定义空白占位视图,继承YSNoDataView

class EV_baseView: YSNoDataView {

    override func ys_addChildView() {
        // 在这里设置空白视图的UI,不用调用super.ys_addChildView()
        // 直接布局
    }
}

2、创建tableView时,注意传入的frame和空白占位视图的相同

private lazy var emptyV:EV_baseView = EV_baseView.createEV(superViewInitBounds: CGRect.tbv_init.resetXY())

private lazy var contentTBV:UITableView = {
    let tbv = UITableView(frame: CGRect.tbv_init)
    // 设置空白占位视图
    tbv.ys_noDataView = self.emptyV 
    return tbv
}

3、隐藏/显示空白占位视图

extension UITableView{

    // 隐藏空白占位视图
    public func ys_hideNoDataView(){}

    // 显示空白占位视图
    public func ys_showNoDataView(){}
}

4、需要刷新时,调用tableView以下的API

// 以下方法不用关心怎么实现,直接调用方法即可
extension UITableView{

    // 在不reloadData的前提下,判断是否应该显示空白占位视图(根据cell的数量)
    public func ys_refreshNoDataView_withoutReloadData(){}

    // reloadData,然后判断是否应该显示空白占位视图
    public func ys_reloadData_noDataView(){}

    // insertSections,然后判断是否应该显示空白占位视图
    public func ys_insertSections_noDataView(_ sections: IndexSet, with: UITableViewRowAnimation){}

    // deleteSections,然后判断是否应该显示空白占位视图
    public func ys_deleteSections_noDataView(_ sections: IndexSet, with: UITableViewRowAnimation){}

    // reloadSections,然后判断是否应该显示空白占位视图
    public func ys_reloadSections_noDataView(_ sections: IndexSet, with: UITableViewRowAnimation){}

    // insertRows,然后判断是否应该显示空白占位视图
    public func ys_insertRows_noDataView(at: [IndexPath], with: UITableViewRowAnimation){}

    // deleteRows,然后判断是否应该显示空白占位视图
    public func ys_deleteRows_noDataView(at: [IndexPath], with: UITableViewRowAnimation){}

    // reloadRows,然后判断是否应该显示空白占位视图
    public func ys_reloadRows_noDataView(at: [IndexPath], with: UITableViewRowAnimation){}
}

5、此外,与 ' 4 ' 同名的方法中,每一个都重载了另一个方法,在参数的最后加入了一个回调,有数据项回调true,无数据项回调false

    completion: @escaping (Bool) -> ()