简体   繁体   中英

Swift 4.2 UITableView Cell doesn't automatically change its height

I have a problem with the auto resizing the height of a cell. There is a UILabel in the cell and it has a long text. However, it is not visible, because the cell doesn't get automatically bigger.

import UIKit 
class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    tableView.estimatedRowHeight = 50
    tableView.rowHeight = UITableView.automaticDimension
  }
}

extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellTableViewCell

    cell.txt.text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. "

    cell.txt.numberOfLines = 0
    cell.txt.lineBreakMode = .byWordWrapping
    cell.txt.sizeToFit()

    return cell
}

}

The result:

 <blockquote class="imgur-embed-pub" lang="en" data-id="a/keAv11b"><a href="//imgur.com/keAv11b"></a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

Could you check it and tell me what is wrong?

By your code, seems to me that you haven't set Delegate and DataSource of your tableView.

tableView.dataSource = self

Besides that,you need to register a custom cell in tableView in viewDidLoad, for example, just like that:

tableView.register(UINib(nibName: "YOUR CELL NAME", bundle: nil), forCellReuseIdentifier: "YOUR CELL REUSE IDENTIFIER")

Please let me know if it works.

First of all you have to set Delegate and DataSource of your tableView :

tableView.dataSource = self
tableView.delegate = self

After doing that be sure that if you are using storyBoard for AutoLayout. UILabel in UITableViewCell have to be constraint all to the edges. I mean top, bottom, leading and trailing constraint set to zero or standard value.

Sometimes setting tableView.estimatedRow and tableView.rowHeight doesn't work so It's better to use delegate function :

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

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