いつもお世話になっております。
UITableViewのセルをカスタムセルとして作っており、そのセル内のlabelの高さに合わせて表示をしたいのですがうまくいきません。
特に、StoryBoardを使用しない方法を書いている記事がほとんどなく難航しております。
調べた所、「セルに対する制約をコーディング」及び「UITableViewAutomaticDimension」を設定することが必要とのことで以下のように制約を記述しているのですが、制約が反映されません(frameで指定した位置にはなる)。高さも可変はするのですが、表示したlabelの下部分がはみ出したりしてしまいます。
また、「短い文章」はテーブルを下にスクロールしたあと上に戻して表示しようとすると、消えてしまいます。「長い文章」もスクロールして再表示したりすると、縦長になってしまうこともあります。
カスタムセルは現状説明ラベルのみで構成されております。
StoryBoardは使用せずコードのみで作成しております。
正しい書き方をご教示いただければと思います。
よろしくお願いいたします。
環境
Xcode8.3.2
Swift3.0
Swift
1 2HomeViewController.swift 3 4import UIKit 5 6class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 7 8 // テーブルビュー 9 var tableView: UITableView! 10 11 // 説明 12 let postTexts = [ 13 "短い文章", 14 "短い文章", 15 "長い文章。特定のセルを選択不可にするというこの設定にはいくつかパターンがある。例えば、このセクションのこのセルだけ、という一部のセルの場合は使えない。", 16 "長い文章\n特定のセルを選択不可にする\nこの設定にはいくつかパターンがある。\n例えば、このセクションのこのセルだけ\nという一部のセルの場合は使えない。", 17 "長い文章。特定のセルを選択不可にするというこの設定にはいくつかパターンがある。例えば、このセクションのこのセルだけ、という一部のセルの場合は使えない。", 18 "長い文章。特定のセルを選択不可にするというこの設定にはいくつかパターンがある。例えば、このセクションのこのセルだけ、という一部のセルの場合は使えない。", 19 "長い文章。特定のセルを選択不可にするというこの設定にはいくつかパターンがある。例えば、このセクションのこのセルだけ、という一部のセルの場合は使えない。", 20 "長い文章。特定のセルを選択不可にするというこの設定にはいくつかパターンがある。例えば、このセクションのこのセルだけ、という一部のセルの場合は使えない。", 21 } 22 23 24 override func viewDidLoad() { 25 super.viewDidLoad() 26 // テーブルビュー初期化、関連付け 27 self.tableView = UITableView() 28 self.tableView.frame = CGRect(x: 0, y: 50, width: scWid, height: scHei*0.7) 29 self.tableView.delegate = self 30 self.tableView.dataSource = self 31 self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "customCell") 32 self.tableView.allowsSelection = false 33 self.view.addSubview(self.tableView) 34 } 35 36 37 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 38 return self.imageNames.count 39 } 40 41 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 42 43 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell 44 cell.myDescriptionLabel.text = self.postTexts[indexPath.row] 45 cell.myDescriptionLabel.sizeToFit() 46 cell.updateConstraintsIfNeeded() 47 return cell 48 } 49 50 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 51 print("セルを選択しました! #(indexPath.row)!") 52 } 53 54 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 55 tableView.estimatedRowHeight = 500.0 56 return UITableViewAutomaticDimension 57 } 58 59 override func didReceiveMemoryWarning() { 60 super.didReceiveMemoryWarning() 61 } 62} 63
Swift
1 2CustomTableViewCell.swift 3 4import UIKit 5 6class CustomTableViewCell: UITableViewCell { 7 8 /// 説明を表示するLabel 9 var myDescriptionLabel: UILabel! 10 11 override init(style: UITableViewCellStyle, reuseIdentifier: String!){ 12 //First Call Super 13 super.init(style: style, reuseIdentifier: reuseIdentifier) 14 15 myDescriptionLabel = UILabel() 16 myDescriptionLabel.frame = CGRect(x: 60, y: 25, width: 240, height: 20) 17 myDescriptionLabel.backgroundColor = UIColor.blue 18 myDescriptionLabel.numberOfLines = 0 19 myDescriptionLabel.lineBreakMode = NSLineBreakMode.byCharWrapping 20 self.addSubview(myDescriptionLabel) 21 22 self.addConstraints([ 23 24 NSLayoutConstraint( 25 item: self.myDescriptionLabel, 26 attribute: .top, 27 relatedBy: .equal, 28 toItem: self, 29 attribute: .top, 30 multiplier: 1.0, 31 constant: 20 32 )] 33 ) 34 self.addConstraints([ 35 36 NSLayoutConstraint( 37 item: self.myDescriptionLabel, 38 attribute: .left, 39 relatedBy: .equal, 40 toItem: self, 41 attribute: .left, 42 multiplier: 1.0, 43 constant: 20 44 )] 45 ) 46 self.addConstraints([ 47 48 NSLayoutConstraint( 49 item: self.myDescriptionLabel, 50 attribute: .right, 51 relatedBy: .equal, 52 toItem: self, 53 attribute: .right, 54 multiplier: 1.0, 55 constant: 20 56 )] 57 ) 58 self.addConstraints([ 59 60 NSLayoutConstraint( 61 item: self.myDescriptionLabel, 62 attribute: .bottom, 63 relatedBy: .equal, 64 toItem: self, 65 attribute: .bottom, 66 multiplier: 1.0, 67 constant: 20 68 )] 69 ) 70 } 71 72 required init(coder aDecoder: NSCoder){ 73 super.init(coder: aDecoder)! 74 } 75 76 override func setSelected(_ selected: Bool, animated: Bool) { 77 super.setSelected(selected, animated: animated) 78 79 // Configure the view for the selected state 80 } 81 82} 83
あなたの回答
tips
プレビュー