1

I am trying to make an app that utilizes the Tableview controller to display some cells. However, my question is: is there a way to display a default screen that says something like "No items scanned" when there are no cells in the tableview? I have attached an image of the type of screen that I am trying to create. Here is an example of what I am trying to create

rice888
  • 51
  • 8

1 Answers1

0

You can use UITableView.backgroundView for that.

Here's an example:

class ViewController: UITableViewController {
    
    ...

    override func numberOfSections(in tableView: UITableView) -> Int {
        if items.isEmpty {
            let label = UILabel()
            label.text = "No Items"
            label.textAlignment = .center
            tableView.backgroundView = label
            tableView.separatorStyle = .none
            return 0
        } else {
            tableView.backgroundView = nil
            tableView.separatorStyle = .singleLine
            return items.count
        }
    }

    ...
}
bcause
  • 1,303
  • 12
  • 29