やりたいこと
xibファイルで作成したセルの中のtextviewの高さを文字量に応じて可変したい
行ったこと
UITextViewのプロパティからScroll ViewのScrollingのScrolling Enabledのチェックを外し、
UITextViewの高さHeight ConstraintのRelationをLess Than or Equalに変更
UITextViewのBottom ConstraintのRelationをGreater Than or Equalに変更
困っていること
オートレイアウトを設定したことにより、textviewの高さが文字量に反応しなくなった(1行分の高さより変わらず)
ViewController
ViewController
1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 6 let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OsakaCell 7 return cell 8 } 9 10 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 11 return 20 12 } 13 14 @IBOutlet weak var table: UITableView! 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 // Do any additional setup after loading the view. 19 table.delegate = self 20 table.dataSource = self 21 22 table.rowHeight = UITableViewAutomaticDimension 23 table.estimatedRowHeight = 10000 24 // Identifierを設定 25 table.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "cell") 26 } 27}
Cell
1import UIKit 2 3class Cell: UITableViewCell,UITextViewDelegate { 4 5 @IBOutlet weak var tv: UITextView! 6 7 override func awakeFromNib() { 8 super.awakeFromNib() 9 // Initialization code 10 tv.delegate = self 11 } 12 13 override func setSelected(_ selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 // Configure the view for the selected state 16 } 17}
追記 textviewのheightの制約を外し、bottomの制約をLess Than or Equalにしたところtextviewの高さが反応するもセルの配置が崩壊。
回答1件
あなたの回答
tips
プレビュー