質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

9480閲覧

UITableViewの高さをセルの内容に応じて可変にする

lyzmfeqpxs54

総合スコア237

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/09/28 02:37

いつもお世話になっております。

UITableViewのセルをカスタムセルとして作っており、そのセルを表示する際に、高さに合わせてUITableViewの高さを変更したいのですがうまくいきません。

調べた所
tableView.rowHeight = UITableViewAutomaticDimension
を使うように書いてある記事が多く以下のように記述したのですが、テーブルビューの高さは変わらず文字が下のセルにはみ出して表示してしまいます。

カスタムセルは左側に画像、右側にタイトルラベルと説明ラベルで構成されております。
StoryBoardは使用せずコードのみで作成しております。

正しい書き方をご教示いただければと思います。
よろしくお願いいたします。

環境
Xcode8.3.2
Swift3.0

Swift

1 2HomeViewController.swift 3 4 5import UIKit 6 7class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 8 9 // テーブルビュー 10 var tableView: UITableView! 11 12 // 画像ファイルの名前 13 let imageNames = ["1.jpeg", "2.jpeg", "3.jpeg", "4.jpeg"] 14 15 // 画像のタイトル 16 let imageTitles = ["A", "B", "C", "D"] 17 18 // 画像の説明 19 let imageDescriptions = [ 20 "短い文章", 21 "短い文章", 22 "長い文章〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜", 23 "長い文章〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜" 24 ] 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 self.view.backgroundColor = UIColor.white 29 30 // テーブルビュー初期化、関連付け 31 tableView = UITableView() 32 tableView.frame = CGRect(x: 0, y: 50, width: 320, height: 400) 33 tableView.delegate = self 34 tableView.dataSource = self 35 tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "customCell") 36 tableView.estimatedRowHeight = 50 37 tableView.rowHeight = UITableViewAutomaticDimension 38 self.view.addSubview(tableView) 39 } 40 41 42 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 43 return self.imageNames.count 44 } 45 46 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 47 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell 48 cell.myImageView.image = UIImage(named: imageNames[indexPath.row]) 49 cell.myTitleLabel.text = self.imageTitles[indexPath.row] 50 cell.myDescriptionLabel.text = self.imageDescriptions[indexPath.row] 51 cell.myDescriptionLabel.sizeToFit() 52 return cell 53 } 54 55 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 56 print("セルを選択しました! #(indexPath.row)!") 57 } 58 59 60 override func didReceiveMemoryWarning() { 61 super.didReceiveMemoryWarning() 62 } 63} 64 65 66 67 68 69CustomTableViewCell.swift 70 71 72 73import UIKit 74 75class CustomTableViewCell: UITableViewCell { 76 77 /// イメージを表示するImageView 78 var myImageView: UIImageView! 79 /// タイトルを表示するLabel 80 var myTitleLabel: UILabel! 81 /// 説明を表示するLabel 82 var myDescriptionLabel: UILabel! 83 84 85 override init(style: UITableViewCellStyle, reuseIdentifier: String!){ 86 //First Call Super 87 super.init(style: style, reuseIdentifier: reuseIdentifier) 88 89 myImageView = UIImageView() 90 myImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 91 myImageView.backgroundColor = UIColor.purple 92 self.addSubview(myImageView) 93 94 myTitleLabel = UILabel() 95 myTitleLabel.frame = CGRect(x: 60, y: 0, width: 100, height: 20) 96 myTitleLabel.backgroundColor = UIColor.red 97 self.addSubview(myTitleLabel) 98 99 myDescriptionLabel = UILabel() 100 myDescriptionLabel.frame = CGRect(x: 60, y: 25, width: 100, height: 20) 101 myDescriptionLabel.backgroundColor = UIColor.blue 102 myDescriptionLabel.numberOfLines = 0 103 self.addSubview(myDescriptionLabel) 104 } 105 106 required init(coder aDecoder: NSCoder){ 107 super.init(coder: aDecoder)! 108 } 109 110 override func setSelected(_ selected: Bool, animated: Bool) { 111 super.setSelected(selected, animated: animated) 112 113 // Configure the view for the selected state 114 } 115 116}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2017/09/28 02:59

「テーブルビューの高さは変わらず」←セルじゃなくて?
lyzmfeqpxs54

2017/09/28 03:04

申し訳ございません。セルの高さです。
guest

回答1

0

cellの中にラベルなどを入れていると思いますが、今回可変にしたいところの高さの大きさを決めていませんか?決めているとセルの大きさは変わらないです。(/ω\)

後ラベルの行数の指定は0にしてありますか?

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { tableView.estimatedRowHeight = 20 //セルの高さ return UITableViewAutomaticDimension //自動設定 }

のような感じにデリーゲートで指定していますか?
ここでの書き方はほんの一例ですが、一番説明しやすかったので書かさせていただきました!!(>ω<)

投稿2017/09/28 12:59

編集2017/09/28 13:07
sachiko-kame

総合スコア334

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

lyzmfeqpxs54

2017/09/28 14:10

ご回答ありがとうございます。 書いてあるコードの通り、特に高さの大きさは決めておらず、ラベルも0行指定しております。ご教示いただいたコードを記述してみたのですが、セルの高さは特に変わらず、ラベルがセルをはみ出して表示している状態です。
sachiko-kame

2017/09/28 14:20

myDescriptionLabel = UILabel() myDescriptionLabel.frame = CGRect(x: 60, y: 25, width: 100, height: 20) myDescriptionLabel.backgroundColor = UIColor.blue myDescriptionLabel.numberOfLines = 0 self.addSubview(myDescriptionLabel) の高さを調節したいんですよね?コードみていませんでしたが、コードできっちり設定しているのでこれだと確かにできなさそうです。コードでなく直接cellに部品をはめてオートレイアウトの設定だと簡単ですが難しそうですか?(>ω<)コードでオートレイアウト書くと大変そうなので……
lyzmfeqpxs54

2017/09/28 15:44

ご回答ありがとうございます。 今回の開発ではコーディングのみで行いたいと考えており、なにか別の方法があればと思ってはいるのですが……。なにか別の方法やアプローチの仕方等教えていただければ幸いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問