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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

7534閲覧

【Swift,Xcode】カスタムセルの高さが変わらない

nekokichi

総合スコア54

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

1クリップ

投稿2018/09/21 14:54

セルに画像とテキストを表示させようと、Xibファイルでカスタムセルを作りました。

画像とテキストがちゃんと表示されるように、RowHeight==300にしました。

しかし、セルの高さがそのままで、セルが崩れて表示されてしまいました。

引用テキスト

念のため、heightForRowAt内に、

swift

1tableView.estimatedRowHeight = 300 2return UITableView.automaticDimension

を付け加えましたが、変わりませんでした。

シミュレータの機種を変更しても、やはりダメでした。

カスタムセルだと高さは変更できないのでしょうか?

もしくは、別の方法があるのでしょうか?

どうかご回答よろしくお願いします。

import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier:"cell") } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell cell.setting(indexPath: indexPath) return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { tableView.estimatedRowHeight = 300 return UITableView.automaticDimension } }
import UIKit class TableViewCell: UITableViewCell { //曲名、アーティスト名、index、画像 var musiclibrary = [["songname":"ヒーリング1","artistname":"魔王魂","index":"0","image":"healing1.jpg"], ["songname":"ヒーリング2","artistname":"魔王魂","index":"1","image":"healing2.jpg"], ["songname":"ヒーリング3","artistname":"魔王魂","index":"2","image": "healing3.jpg"], ["songname":"バトル1","artistname":"魔王魂","index":"3","image":"battle.jpg"], ["songname":"サイバー","artistname":"魔王魂","index":"4","image": "cyber.jpg"]] @IBOutlet weak var imageview: UIImageView! @IBOutlet weak var songname: UILabel! @IBOutlet weak var artistname: UILabel! override func awakeFromNib() { super.awakeFromNib() } //ラベルと画像の内容 func setting(indexPath: IndexPath) { imageview.image = UIImage(named: musiclibrary[indexPath.row]["image"]!) songname.text = musiclibrary[indexPath.row]["songname"] artistname.text = musiclibrary[indexPath.row]["artistname"] } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }

--TableViewCell.xib
イメージ説明

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

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

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

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

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

guest

回答1

0

ベストアンサー

heightForRowAtの戻り値として高さを返してください。
固定なら単純に return 300 でいいです。

estimatedRowHeight はアイテムの高さが不揃いかつ数が多くてテーブル全体の高さが最初に正確に計算できない場合に、スクロールバーの位置などを概算で計算するための値で用途が違います。

投稿2018/09/21 15:25

toki_td

総合スコア2850

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

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

nekokichi

2018/09/21 15:50

ありがとうございます! 初歩的なミスでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問