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

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

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

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

Swift

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

Q&A

解決済

1回答

2021閲覧

【Swift,Xcode】セルの伸び縮み表示

nekokichi

総合スコア54

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/10/03 15:18

編集2018/10/04 05:12

セルをタップすると伸び縮みする機能を実装したいのですが、
・セルに色が反映されない
・セルをタップしても伸び縮みしない
という問題に直面しています。

イメージ説明

カスタムセルを用いているので、ViewControllerのviewDidLoadでtableViewに登録しているはずなのにです。

delegata,datasourceに必要なtableViewのメソッドは全て記述してるか確認しましたが、どこも異常はありませんでした。

下記の記事のサンプルコードと同じに書いたのですが、ダメでした。

Qiita そのまま使える!iOSアプリを作るためのswiftサンプル集

同じコードなのに動作しないのは、Swift4から仕様が変わったからでしょうか?

Qiitaの記事は最終更新日が比較的新しいので、コード的にも今の環境に適していると思うのですが、そうでなければ僕のコードが間違っているなのでしょうが、どこが間違いかわかりません。

どうかダメなのか指摘していただきたいです。

よろしくお願いします。

Swift

1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 4 5 6 @IBOutlet weak var tableView: UITableView! 7 8 private let dataSource: [UIColor] = [.red, .green, .blue, .cyan, .yellow, .magenta] 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 //カスタムセルを登録する 14 tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cell") 15 16 } 17 18 func numberOfSections(in tableView: UITableView) -> Int { 19 return 1 20 } 21 22 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 23 return dataSource.count 24 } 25 26 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 27 28 //gurad文でカスタムセルを読み込み、無理ならUITableViewCell()を返す 29 guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell else { return UITableViewCell() } 30 31 cell.backgroundColor = dataSource[indexPath.row] 32 33 return cell 34 35 } 36 37 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 38 39 // 40 guard let cell = tableView.cellForRow(at: indexPath) as? TableViewCell else { return } 41 42 //??? 43 tableView.beginUpdates() 44 // 45 cell.flexible() 46 //??? 47 tableView.endUpdates() 48 49 } 50 51 52} 53 54

Swift

1import UIKit 2 3class TableViewCell: UITableViewCell { 4 5 //ConstraintをOutletで紐付け 6 @IBOutlet weak var cellHeight: NSLayoutConstraint! 7 8 override func awakeFromNib() { 9 super.awakeFromNib() 10 // Initialization code 11 } 12 13 override func setSelected(_ selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 16 // Configure the view for the selected state 17 } 18 19 func flexible() { 20 //セルの高さ(constraints)を変更する 21 //44なら100,44でないなら44 22 cellHeight.constant = cellHeight.constant == 44 ? 100 : 44 23 } 24 25} 26

TableViewCell.xib
イメージ説明

※追記
ご回答通りに記述してもだめだったので、

Swift

1func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 3 //gurad文でカスタムセルを読み込み、無理ならUITableViewCell()を返す 4 guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell else { return UITableViewCell() } 5 6// cell.backgroundColor = dataSource[indexPath.row] 7 cell.contentView.backgroundColor = dataSource[indexPath.row] 8 9 return cell 10 11 }

と書きましたが、だめでした。

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

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

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

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

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

guest

回答1

0

ベストアンサー

cell.backgroundColorではなくcell.contentView.backgroundColorにして下さい。

と思ったけど、その上にViewが乗ってるみたいですね‥。
そのViewの.backgroundColorを変更して下さい。

とりあえずこれでいけるかも。

swift

1cell.contentView.subviews.first?.backgroundColor = dataSource[indexPath.row]

投稿2018/10/04 00:33

編集2018/10/04 00:44
fuzzball

総合スコア16731

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

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

fuzzball

2018/10/04 05:32

そもそもtableView(_:cellForRowAt:)は呼ばれていますか? dataSourceは設定していますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問