Dynamically increase height of UILabel & TableView Cell?

前端 未结 7 1702
广开言路
广开言路 2020-12-01 12:52

I have a UITableView in which i am displaying a custom cell.I my cell i have two label & one view as below in picture.

I have given constraint of left v

7条回答
  •  离开以前
    2020-12-01 13:22

    Swift 3 / Swift 4

    Custom cell:

    class CustomCell: UITableViewCell {
    
        @IBOutlet var messageLabel: UILabel!
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            sizeToFit()
            layoutIfNeeded()
    
        }
    
    }
    

    viewDidLoad:

    override func viewDidLoad() {
        super.viewDidLoad()        
    
        self.tableView.estimatedRowHeight = 80
        self.tableView.rowHeight = UITableViewAutomaticDimension
    
    }
    

    And cellForRowAtIndexPath:

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
         let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifier", for: indexPath) as! CustomCell
         cell.messageLabel.text = exampleContent[indexPath.row]
         cell.messageLabel.sizeToFit()
         cell.messageLabel.layoutIfNeeded()
         return cell
    

    }

提交回复
热议问题