UITableViewCell auto height based on amount of UILabel text

前端 未结 5 1427
终归单人心
终归单人心 2020-12-03 20:54

I have a bit of a tricky set up in my storyboard, I have a UIViewController that holds a UITableViewController. Within the UITableViewController I have several prototypecell

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 21:55

    Swift 4.2 and 5. As mention above you can set UITableView object property or

    tblView.rowHeight = UITableView.automaticDimension 
    tblView.estimatedRowHeight = 300
    

    you can also define the same by implementing UITableViewDelegate methods

    extension ViewController: UITableViewDelegate {
          func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return 300
          }
          func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
          }
        }
    

提交回复
热议问题