2016-09-11 6 views
1

estimatedRowHeightを定数に設定し、rowHeightUITableViewAutomaticDimensionに設定しましたが、テーブルビューのセルがまだセルのdetailTextLabelにサイズ変更されていません。どこが間違っていますか?セルフサイジングテーブルビューのセルが機能しない

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.estimatedRowHeight = 130.0 
    tableView.tableFooterView = UIView() 
    tableView.separatorInset.left = 50 
    tableView.registerClass(CommentCellView.self, forCellReuseIdentifier: cellid) 
    tableView.rowHeight = UITableViewAutomaticDimension 


} 

override func viewDidAppear(animated: Bool) { 
    tableView.reloadData() 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellid, forIndexPath: indexPath) as! CommentCellView 

    return cell 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 

} 

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 60.0 
} 

のUITableViewCellクラス:(この例ではyourTextと呼ばれる)、ラベル

var yourText : UILabel = { 
    let label = UILabel() 
    label.translatesAutoresizingMaskIntoConstraints = false 
    return label 
}() 

はこの方法であなたのセルの制約を設定し、置く設定し

class CommentCellView: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier) 

     detailTextLabel?.numberOfLines = 0 
     detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." 

} 

enter image description here

+0

テーブルビューセルに適切な自動レイアウト制約がありますか? – jcaron

+0

'estimatedHeightForRowAtIndexPath'関数を削除しようとしています – iamalizade

答えて

2

setupComponents方法はこちら:

required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     setupComponents() 
    } 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: .Subtitle, reuseIdentifier: "CellId") 

     setupComponents() 

    } 

     func setupComponents(){ 



    self.addSubview(yourText) 

    yourText.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 6).active = true 
      yourText.centerYAnchor.constraintEqualToAnchor(self.centerYAnchor).active = true 
      yourText.widthAnchor.constraintEqualToConstant(110).active = true 

    yourText.numberOfLines = 0 

    yourText.lineBreakMode = NSLineBreakMode.ByWordWrapping 
    self.bottomAnchor.constraintEqualToAnchor(yourText.bottomAnchor, constant: 5).active = true 
    } 
関連する問題