Skip to content

Instantly share code, notes, and snippets.

@tesddev
Created May 23, 2023 10:20
Show Gist options
  • Save tesddev/5ca2dc8ea9a6d28b29df3491ad8f26ee to your computer and use it in GitHub Desktop.
Save tesddev/5ca2dc8ea9a6d28b29df3491ad8f26ee to your computer and use it in GitHub Desktop.
Snippet For Empty List TableView Background View
/// define label or image as needed, here, an image
var emptyListLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = .black
label.text = "No shipment with the selected filter!"
label.font = UIFont.systemFont(ofSize: 11)
label.numberOfLines = 0
return label
}()
/// inside a table view data source delegate function
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if model.count > 0 {
emptyListLabel.removeFromSuperview()
return model.count
}
else{
tableView.addSubview(emptyListLabel)
NSLayoutConstraint.activate([
emptyListLabel.centerXAnchor.constraint(equalTo: tableView.centerXAnchor),
emptyListLabel.centerYAnchor.constraint(equalTo: tableView.centerYAnchor),
])
tableView.backgroundView = emptyListLabel
tableView.separatorStyle = .none
return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment