お世話になります。
Swift初心者です。
お忙しい中大変申し訳ございませんが、
現在、UITableViewの実装で悩んでおります。
セルの高さの自動調整についてのご質問となります。
前提・実現したいこと
セルの構成として、
セルの左半分にLabel、右半分にTextViewを配置し、
Labelのテキストは可変長(テキスト長に応じて高さが変動)、TextViewの高さは固定(高さ:300)として、
・Labelのテキスト長が少ない(Labelの高さ<TextViewの高さ)の場合 → セルの高さをTextViewの高さとする
・Labelのテキスト長が多い(Labelの高さ>TextViewの高さ)の場合 → セルの高さをLabelの高さとする
を考えております。
発生している問題・エラーメッセージ
書籍やWebより、
セルの高さの自動調整として、
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { freeSetsumon.estimatedRowHeight = 150 return UITableViewAutomaticDimension }
の処理で高さが自動で算出され、
かつ、その際、LabelもしくはTextViewのいずれかに制限をかけなければならないというのは
何となくわかってきてはいるのですが、実装上どのようにすれば上記が実現できるのかがわからず
困っている状況でございます。
実装方法について、ご存知の方がおりましたら、
ご助力を賜りたく、ご教授頂けますと幸いでございます。
ご迷惑をおかけ
どうぞ、よろしくお願い致します。
該当のソースコード
▼Label側(Viewの中にLabelを配置) Labelはstoryboard上でLines=0としております // View include Label labelBaseView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ labelBaseView.widthAnchor.constraint(equalToConstant: 400.0), labelBaseView.topAnchor.constraint(equalTo: cell.topAnchor, constant: 5.0), labelBaseView.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 10.0), labelBaseView.bottomAnchor.constraint(equalTo: cell.bottomAnchor, constant: 0.0) ]) // Label labelName.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ labelName.topAnchor.constraint(equalTo: labelBaseView.topAnchor, constant: 0.0), labelName.leadingAnchor.constraint(equalTo: labelBaseView.leadingAnchor, constant: 0.0), labelName.trailingAnchor.constraint(equalTo: labelBaseView.trailingAnchor, constant: 0.0), labelName.bottomAnchor.constraint(equalTo: labelBaseView.bottomAnchor, constant: 0.0) ]) ▼TextView側(Viewの中にTextViewを配置) // View include TextView textBaseView.translatesAutoresizingMaskIntoConstraints = false textBaseView.heightAnchor.constraint(equalToConstant: 300.0).isActive = true textBaseView.topAnchor.constraint(equalTo: cell.topAnchor, constant: 10.0).isActive = true textBaseView.leadingAnchor.constraint(equalTo: labelBaseView.trailingAnchor, constant: 8.0).isActive = true textBaseView.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10.0).isActive = true if labelHeight < 300.0 { inputBaseView.bottomAnchor.constraint(equalTo: cell.bottomAnchor, constant: -10.0).isActive = true } // TextView textView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ textView.topAnchor.constraint(equalTo: textBaseView.topAnchor), textView.bottomAnchor.constraint(equalTo: textBaseView.bottomAnchor), textView.leadingAnchor.constraint(equalTo: textBaseView.leadingAnchor), textView.trailingAnchor.constraint(equalTo: textBaseView.trailingAnchor) ]) ▼実行箇所 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! sampleTableViewCell cell.labelName.text = "1234567890" ★ここでLabel側の制約処理を実行しています return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { sampleTableView.estimatedRowHeight = 150 return UITableViewAutomaticDimension } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { tableView.beginUpdates() ★ここでTextView側の制約処理を実行しています tableView.estimatedRowHeight = 150 tableView.rowHeight = UITableViewAutomaticDimension tableView.endUpdates() }
試したこと
補足情報(FW/ツールのバージョンなど)
Swift4
xCode10.0
回答1件
あなたの回答
tips
プレビュー