iPhoneアプリの実装をしていたらtextLabelとdetailTextLabelがdeprecatedになっていたので詳しく調べてみました。
textLabelとdetailTextLabelはUITableViewCellの左右のラベルにアクセスできるプロパティーです。
警告は下の通りです。
代わりにUIListContentConfigurationを利用するように書かれていました。
Use UIListContentConfiguration instead, this property will be deprecated in a future release.
UIListContentConfigurationとは
UIListContentConfigurationはiOS14から登場した構造体でUITableViewCellのレイアウトを決めるものです。
使い方は下の通りです。
UIListContentConfigurationのインスタンスを作成してテキストなどを設定してからcontentConfigurationにセットしています。
var content = UIListContentConfiguration.cell() content.text = "Test" cell.contentConfiguration = content
実行すると左側にラベルが置かれたTableViewのCellが表示されます。
右側にもラベルを表示したい場合はvalueCellを使います。
var content = UIListContentConfiguration.valueCell() content.text = "Test" content.secondaryText = "Secondary" cell.contentConfiguration = content
実行すると下のような表示になります。
subtitleCellを使うと下のような表示も可能です。
var content = UIListContentConfiguration.subtitleCell() content.text = "Test" content.secondaryText = "Secondary" cell.contentConfiguration = content
テキストだけでなく画像の設置も可能です。
var content = UIListContentConfiguration.subtitleCell() content.text = "Test" content.secondaryText = "Secondary" content.image = UIImage(named: "Image") cell.contentConfiguration = content
文字のサイズなどはtextPropertiesを通して変更する事ができます。
他にも画像のプロパティーを操作したりNSAttributedStringをセットすることなどもできるようです。
var content = UIListContentConfiguration.subtitleCell() content.textProperties.font = UIFont.boldSystemFont(ofSize: 20) cell.contentConfiguration = content
古いやる方が非推奨になったので、今後はiOS13非対応のアプリを直すときはこちらを積極的に使おうと思いました。