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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Swift

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

Q&A

解決済

1回答

204閲覧

二つのtableViewCellを別々の高さに設定する

anddy

総合スコア14

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

Swift

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

0グッド

1クリップ

投稿2019/09/01 09:17

二つのtableViewCellのそれぞれの高さを設定したい

swiftでApple music APIを使ったコードを書いています

発生している問題・エラーメッセージ

一方のセルの高さを40に、もう一方を140に設定したいのですが、両方40になって、一方のセルが途中で切れている

該当のソースコード

swift

1 var canMusicCatalogPlayback = false 2 override func viewDidLoad() { 3 super.viewDidLoad() 4 tableView.delegate = self 5 tableView.dataSource = self 6 7 prepare() 8 } 9 func prepare() { 10 guard albumID == nil else{ 11 apiClient.album(id: albumID!) { [unowned self] album in 12 DispatchQueue.main.async { 13 self.album = album 14 self.tableView.reloadData() 15 } 16 } 17 return 18 } 19 20 self.cloudServiceController.requestCapabilities { capabilities, error in 21 guard capabilities.contains(.musicCatalogPlayback) else { return } 22 self.canMusicCatalogPlayback = true 23 } 24 } 25} 26 27extension AlbumViewController:UITableViewDataSource,UITableViewDelegate{ 28 func numberOfSections(in tableView: UITableView) -> Int { 29 guard album != nil else { return 0 } 30 return 2 31 } 32 33 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 34 guard let album = album else { return 0 } 35 if section == 0 { 36 return 1 37 } else { 38 return album.relationships!.tracks!.count 39 } 40 } 41 42 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 43 if indexPath.section == 0 { 44 let cell = tableView.dequeueReusableCell(withIdentifier: "AlbumHeaderCell", for: indexPath) as! AlbumHeaderCell 45 cell.nameLabel.text = album!.attributes?.name 46 cell.artistLabel.text = album!.attributes?.artistName 47 cell.yearlabel.text = album!.attributes?.releaseDate 48 if let url = album!.attributes?.artwork?.imageURL(width: 220, height: 220) { 49 apiClient.image(url: url) { image in 50 cell.thumbnailView.image = image 51 } 52 } 53 return cell 54 } 55 56 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 57 let track = album?.relationships!.tracks![indexPath.row] 58 cell.textLabel?.text = track?.attributes?.name 59 return cell 60 } 61 62 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 63 64 tableView.estimatedRowHeight = 40 //セルの高さ 65 return UITableView.automaticDimension //自動設定 66 } 67 68 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 69 tableView.deselectRow(at: indexPath, animated: true) 70 guard canMusicCatalogPlayback else { return } 71 guard indexPath.section == 1 else { return } 72 73 } 74}

試したこと

storyboardでそれぞれのセルの高さを調整したり、コードをいろいろ試してみましたが、一向にできません。。。。。

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

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

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

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

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

guest

回答1

0

ベストアンサー

簡単な方法はstoryboardでtableviewcellにそれぞれ高さを設定することだと思います。

他にはコードを拝見すると、、、

Swift

1func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 2 if indexPath.section == 0 { 3 return 140 4 } else { 5 return 40 6 } 7}

投稿2019/09/01 11:01

編集2019/09/01 16:08
hameji

総合スコア1380

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

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

anddy

2019/09/02 01:41 編集

ご回答ありがとうございます! storyboardではできなかったんですが、コードを書けばいけました ありがとうございました!!!!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問