0
0
iOS Swiftmobile~5 mins

Empty state handling in iOS Swift

Choose your learning style9 modes available
Introduction

Empty state handling shows a friendly message or image when there is no data to display. It helps users understand what to do next instead of seeing a blank screen.

When a list or table has no items to show yet.
When a search returns no results.
When a user has not added any content or data.
When an app feature is temporarily unavailable or empty.
When waiting for data to load but nothing is ready to display.
Syntax
iOS Swift
if dataArray.isEmpty {
    // Show empty state view
} else {
    // Show data list view
}

Check if your data source is empty using isEmpty.

Display a special view or message to guide the user when empty.

Examples
Show or hide a label depending on whether the items array is empty.
iOS Swift
if items.isEmpty {
    emptyLabel.isHidden = false
} else {
    emptyLabel.isHidden = true
}
Set a custom empty view as the background of a table when there are no items.
iOS Swift
tableView.backgroundView = items.isEmpty ? emptyView : nil
Use guard to exit early and show empty state if data is empty.
iOS Swift
guard !data.isEmpty else {
    showEmptyState()
    return
}
Sample App

This example shows a table view that displays a list of items. If the list is empty, it hides the table and shows a friendly message label in the center.

iOS Swift
import UIKit

class EmptyStateViewController: UIViewController, UITableViewDataSource {
    let tableView = UITableView()
    let emptyLabel = UILabel()
    var items: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        emptyLabel.text = "No items found. Please add some."
        emptyLabel.textAlignment = .center
        emptyLabel.textColor = .gray
        emptyLabel.translatesAutoresizingMaskIntoConstraints = false

        tableView.dataSource = self
        tableView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(tableView)
        view.addSubview(emptyLabel)

        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

            emptyLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            emptyLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

        updateUI()
    }

    func updateUI() {
        if items.isEmpty {
            tableView.isHidden = true
            emptyLabel.isHidden = false
        } else {
            tableView.isHidden = false
            emptyLabel.isHidden = true
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }
}
OutputSuccess
Important Notes

Always provide a clear message or image to help users understand the empty state.

Consider adding a button or action to help users add data or refresh.

Keep empty state UI simple and visually distinct from normal content.

Summary

Empty state handling improves user experience by showing helpful messages when no data is available.

Use isEmpty to check data and toggle views accordingly.

Design empty states to guide users on what to do next.