1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Swift]TableViewのCellがあるかの判別(0件の時に文字を表示)

Posted at

はじめに

下記のようなToDo作成画面で、Cellがない時は「登録しているToDoがありません」と表示させることが多いと思います。

ezgif-5-5bbc20ca27.gif

調べてると意外と情報が少なかったので載せておきます。

方法

まず、TableViewの下に表示させたいLabelを配置しておきます。

スクリーンショット 2022-05-19 18.09.29.png

次に、TableViewの「numberOfRowsInSection」部分のセットアップで下記を記載します。

TodoViewController.swift

private var todoModel: [TodoModel] = []

      #//省略

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if todoModel.isEmpty {
            tableView.backgroundColor = .clear
        } else {
            tableView.backgroundColor = .white
        }
        return todoModel.count
    }

cellがない場合はTableViewのbackgroundColorを透明にして
cellがある場合は白にすればいいだけ。

簡単ですね。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?