简体   繁体   中英

Dynamically change cell height according to label text

I tried to add Q&A part for my app and that tableview cell have to expand according to text height, cell height have to change with this text. In this point I did some thing to solve problem which are numberOfline = 0 and UITableView.automaticDimension code can't solve my problem. Here my code from project :

Problem is cell height didn't change this code just write label under the questions and for longer answers under the next question. Means cell height didn't change.

let soruLabel: UILabel = {
    let sorulabel = UILabel()
    sorulabel.textColor = .darkGray
    sorulabel.numberOfLines = 0
    sorulabel.font = UIFont.boldSystemFont(ofSize: 17)
    return sorulabel
}()

let cevapLabel: UILabel = {
    let cevaplabel = UILabel()
    cevaplabel.textColor = .black
    cevaplabel.numberOfLines = 0
    cevaplabel.font = UIFont(name: "HelveticaNeue", size: 18)
    return cevaplabel
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setup()
}

func set(content: expandingTableCellForsss) {
    self.soruLabel.text = content.soru
    self.cevapLabel.text = content.expanded ? content.cevap : ""
}

func setup() {
    backgroundColor = UIColor.init(red: 245, green: 245, blue: 245, alpha: 1)


    addSubview(soruLabel)
    addSubview(cevapLabel)


    soruLabel.translatesAutoresizingMaskIntoConstraints = false
    soruLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
    soruLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    soruLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = false
    soruLabel.rightAnchor.constraint(equalTo: rightAnchor).isActive = true

    cevapLabel.translatesAutoresizingMaskIntoConstraints = false
    cevapLabel.topAnchor.constraint(equalTo: soruLabel.bottomAnchor).isActive = true
    cevapLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    cevapLabel.bottomAnchor.constraint(equalTo: soruLabel.bottomAnchor).isActive = false
    cevapLabel.rightAnchor.constraint(equalTo: rightAnchor).isActive = true




}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

Please check : UITableViewCell auto height based on amount of UILabel text ,

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

To use dynamic cell height: 1) give a minimum row height for cell in viewDidLoad

tableView.estimatedRowHeight = 300

2) set rowHeight property of tableview as automatic in viewDidLoad

tableView.rowHeight = UITableViewAutomaticDimension

3) in storyboard select numberOfLines = 0

4) if you have provided custom height to cell in storyBoard, make sure its equal to estimated row height.

需要设置底部锚点:

cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM