Add an empty view to your table view with one line of codes.
- Add an empty view to a table view or a view.
Just drag the DJEmptyView
folder to your project.
pod 'DJEmptyView'
You should also import this framework before you use it, like this import DJEmptyView
.
It's very simple. only one function you should update. In your loadData
function, you can just do this.
private func loadData() {
MyService.request(target: .dataList, success: { (res) -> (Void) in
// empty view start.
if dataArray.count == 0 {
self.tableView.reloadData()
self.tableView.dj_showEmptyView(DJEmptyView(tipInfo: "no data", imageName: "img_no_data"))
return
}
self.tableView.dj_hideEmptyView()
// empty view end.
// more codes
})
}
You do not need add any codes to support this function. You can just drag down the empty view.
Tips: You should add below codes to your table view, otherwise there will be lines in the empty view.
tableView.separatorStyle = .none
You can download the demo for example codes.
MIT