LoginSignup
0
0

More than 1 year has passed since last update.

TableViewのtextLabelの高さを可変にしたい!!

Posted at

はじめに

TableViewのCellの高さを可変にしたい時、調べるとCustomCellを使ったやり方がほぼ紹介されていて、デフォルトでのTableViewを使ったものが見つからなかったので、備忘録的なメモです。

高さ可変前

前提として、

ViewController
let LabelArray = ["LabelLabelLabelLabelLabelLabelLabelLabelLabelLabelLabelLabel","Label","Label"]

を定義します。

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

このままでは1番目の配列の最後尾が...で省略されてしまいます。

高さ可変する!!

ViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = LabelArray[indexPath.row]
        //この一行を入れるだけ!!
        cell.textLabel?.numberOfLines = 0
        return cell
    }
}

これで高さが可変になり、文字も最後まで表示する事が出来ました!!

0
0
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
0
0