いつもお世話になっております。
UITableViewのセルをカスタムセルとして作っており、そのセルを表示する際に、高さに合わせてUITableViewの高さを変更したいのですがうまくいきません。
調べた所
tableView.rowHeight = UITableViewAutomaticDimension
を使うように書いてある記事が多く以下のように記述したのですが、テーブルビューの高さは変わらず文字が下のセルにはみ出して表示してしまいます。
カスタムセルは左側に画像、右側にタイトルラベルと説明ラベルで構成されております。
StoryBoardは使用せずコードのみで作成しております。
正しい書き方をご教示いただければと思います。
よろしくお願いいたします。
環境
Xcode8.3.2
Swift3.0
Swift
HomeViewController.swift import UIKit class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // テーブルビュー var tableView: UITableView! // 画像ファイルの名前 let imageNames = ["1.jpeg", "2.jpeg", "3.jpeg", "4.jpeg"] // 画像のタイトル let imageTitles = ["A", "B", "C", "D"] // 画像の説明 let imageDescriptions = [ "短い文章", "短い文章", "長い文章〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜", "長い文章〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜" ] override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white // テーブルビュー初期化、関連付け tableView = UITableView() tableView.frame = CGRect(x: 0, y: 50, width: 320, height: 400) tableView.delegate = self tableView.dataSource = self tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "customCell") tableView.estimatedRowHeight = 50 tableView.rowHeight = UITableViewAutomaticDimension self.view.addSubview(tableView) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.imageNames.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell cell.myImageView.image = UIImage(named: imageNames[indexPath.row]) cell.myTitleLabel.text = self.imageTitles[indexPath.row] cell.myDescriptionLabel.text = self.imageDescriptions[indexPath.row] cell.myDescriptionLabel.sizeToFit() return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("セルを選択しました! #(indexPath.row)!") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } CustomTableViewCell.swift import UIKit class CustomTableViewCell: UITableViewCell { /// イメージを表示するImageView var myImageView: UIImageView! /// タイトルを表示するLabel var myTitleLabel: UILabel! /// 説明を表示するLabel var myDescriptionLabel: UILabel! override init(style: UITableViewCellStyle, reuseIdentifier: String!){ //First Call Super super.init(style: style, reuseIdentifier: reuseIdentifier) myImageView = UIImageView() myImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) myImageView.backgroundColor = UIColor.purple self.addSubview(myImageView) myTitleLabel = UILabel() myTitleLabel.frame = CGRect(x: 60, y: 0, width: 100, height: 20) myTitleLabel.backgroundColor = UIColor.red self.addSubview(myTitleLabel) myDescriptionLabel = UILabel() myDescriptionLabel.frame = CGRect(x: 60, y: 25, width: 100, height: 20) myDescriptionLabel.backgroundColor = UIColor.blue myDescriptionLabel.numberOfLines = 0 self.addSubview(myDescriptionLabel) } required init(coder aDecoder: NSCoder){ super.init(coder: aDecoder)! } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
まだ回答がついていません
会員登録して回答してみよう