如何设置自动调整大小的单元格 iOS8

How to set up self sizing cells iOS8

我有一个带有 table 视图的视图控制器。现在我想设置一个自动调整大小的单元格。单元格应包含三个或更多标签。 如果我只有两个标签,则自动调整大小的单元格可以完美运行。但是一旦我添加第三个标签,第二个标签中的文本就不再换行。而在第三个标签下面是很多免费的space。 Screenshot http://stefangerard.com/images/selfSizing.png 以上就是三个Label的约束。

constraint first Label http://stefangerard.com/images/constraint1.png

constraint second Label http://stefangerard.com/images/constraint2.png

constarint third Label http://stefangerard.com/images/constraint3.png

在我的 ViewController 中,我只是设置单元格并在 viewDidLoad() 中调用这两行,单元格自行计算其高度。

self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension

我的 ViewController 看起来像这样:

import UIKit
class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 50.0
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.tableView.reloadData()
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var someRandomText1 = "Far curiosity incommode now led smallness allowance. Favour bed assure son things yet. She consisted consulted elsewhere happiness disposing household any old the. Widow downs you new shade drift hopes small. So otherwise commanded sweetness we improving. Instantly by daughters resembled unwilling principle so middleton. Fail most room even gone her end like. Comparison dissimilar unpleasant six compliment two unpleasing any add. Ashamed my company thought wishing colonel it prevent he in. Pretended residence are something far engrossed old off."
    var someRandomText2 = "Concerns greatest margaret him absolute entrance nay. Door neat week do find past he. Be no surprise he honoured indulged. Unpacked endeavor six steepest had husbands her. Painted no or affixed it so civilly. Exposed neither pressed so cottage as proceed at offices. Nay they gone sir game four. Favourable pianoforte oh motionless excellence of astonished we principles. Warrant present garrets limited cordial in inquiry to. Supported me sweetness behaviour shameless excellent so arranging."

    var cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
    cell.headlineLabel.text = "Hello CustomCell"
    cell.text1Label.text = someRandomText1
    cell.text2Label.text = someRandomText2
    return cell
}
}

谁能帮我解决这个问题?

谢谢!

编辑

您可以在这里下载项目: https://github.com/stefocdp/selfSizingCell

我认为您的问题来自 estimatedRowHeight。你必须把它设置得更接近现实中的样子。

在您当前的约束条件下,只有标签之间的 space 为 48。我也遇到了这个问题,如果不将 estimatedRowHeight 设置为合理的数字会使 tableview 不再计算任何有用的东西。

尝试将其设置为 110(每个标签 3 x 20 + 它们之间的 48)。

我通过将这行代码添加到 cellForRowAtIndexPath 函数来修复它

cell.layoutIfNeeded()

我的 ViewController 现在看起来像这样:

import UIKit

class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 50
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.tableView.reloadData()
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var someRandomText1 = "Far curiosity incommode now led smallness allowance. Favour bed assure son things yet. She consisted consulted elsewhere happiness disposing household any old the. Widow downs you new shade drift hopes small. So otherwise commanded sweetness we improving. Instantly by daughters resembled unwilling principle so middleton. Fail most room even gone her end like. Comparison dissimilar unpleasant six compliment two unpleasing any add. Ashamed my company thought wishing colonel it prevent he in. Pretended residence are something far engrossed old off."
    var someRandomText2 = "Concerns greatest margaret him absolute entrance nay. Door neat week do find past he. Be no surprise he honoured indulged. Unpacked endeavor six steepest had husbands her. Painted no or affixed it so civilly. Exposed neither pressed so cottage as proceed at offices. Nay they gone sir game four. Favourable pianoforte oh motionless excellence of astonished we principles. Warrant present garrets limited cordial in inquiry to. Supported me sweetness behaviour shameless excellent so arranging."

    var cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
    cell.headlineLabel.text = "Hello CustomCell"
    cell.text1Label.text = someRandomText1
    cell.text2Label.text = someRandomText2
    cell.layoutIfNeeded()
    return cell
}
}