如何设置自定义单元格iOS8

时间:2015-04-28 18:06:34

标签: ios iphone uitableview swift autolayout

我有一个带有表视图的视图控制器。现在我想建立一个自定型单元格。单元格应包含三个或更多标签。 如果我只有两个标签,那么自定型单元可以完美地工作。但是一旦我添加第三个Label,第二个标签中的文本就不再包装了。第三个标签下面是很多可用空间。 Screenshot http://stefangerard.com/images/selfSizing.png 这些是三个标签的限制。

  • 第一个Label(Hello CustomCell)

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

2 个答案:

答案 0 :(得分:2)

我通过将这行代码添加到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
}
}

答案 1 :(得分:1)

我相信你的问题来自estimatedRowHeight。你必须让它更接近现实。

在您使用当前约束的情况下,只有标签之间的空格才能生成48.我也遇到了这个问题,没有将estimatedRowHeight设置为合理的数字会使tableview不再计算任何好处。

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