3
4

More than 1 year has passed since last update.

iOS14で非推奨となったUITableViewCellのtextLabelプロパティ等の対応(iOS13対応含)

Last updated at Posted at 2021-10-31

経緯

iOS14でUITableViewCellのtextLabelプロパティ等が非推奨(Deprecated)になりました。代わりに、defaultContentConfiguration等を使用するのですが、こちらはiOS13非対応のため、現状iOS13以前に対応しているアプリだと使えません。

Xcodeが助け舟を出してくれますが、念の為解決方法をメモしておきます。

対応方法

// クラス名は任意。TableViewControllerを使わない場合、親クラスはUITableViewDataSource
class TableViewController: UITableViewController {

    let itemArray = ["A", "B", "C"]

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // withIdentifierは任意
        let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
        if #available(iOS 14.0, *) {
            // iOS14以降の推奨
            var content = cell.defaultContentConfiguration()
            content.text = itemArray[indexPath.row]
            cell.contentConfiguration = content
        } else {
            // iOS13以前
            cell.textLabel?.text = itemArray[indexPath.row]
        }
        return cell
    }
}

結果

(これまでと変わりません)
スクリーンショット 2021-10-31 11.30.22.png

参考

3
4
1

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
3
4