Created
May 23, 2023 10:20
-
-
Save tesddev/5ca2dc8ea9a6d28b29df3491ad8f26ee to your computer and use it in GitHub Desktop.
Snippet For Empty List TableView Background View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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