使用自动大小类的方式在iOS 8和Swift中实现UITableViewCell中多行UILabel

5
我遇到了iOS 8 beta 4(beta 5也一样)的问题,即UILabel在UITableViewCells中没有显示所有文本。我使用了自适应大小单元格来实现这一点,并为所有设备使用一个故事板。
以下是iPhone上的效果:
图片
代码:
ViewController.swift
@IBOutlet var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cell")

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


func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as TableViewCell


    switch indexPath.row {
    case 0:
        cell.label.text  = "hello hello hello hellohellohellohellohello hello hello hello hello hello hello hello hellohellohello hellohello hellohellohellohellohello hello hello hello hello hello hello hello hello hello hello hello hello hellohellohello hello hello hello"
    case 1:
        cell.label.text  = "doesn'twork doesn'tworkdoesn't work doesn'twork doesn'tworkdoesn't work doesn'twork doesn'twork doesn't workdoesn't workdoesn'twork doesn'twork doesn't workdoesn't workdoesn't workdoesn'twork "
    case 2:
        cell.label.text  = "baims baimsbaimsbaims baimsbaimsbaimsbaims baims baimsbaims baimsbaimsbaimsbaims baimsbaims baims baimsbaimsbaims baimsbaims baims baimsbaimsbaimsbaims baimsbaims baimsbaims baimsbaims"
    default:
        cell.label.text  = "hello hello hello hellohellohellohellohello hello hello hello hello hello hello hello hellohellohello hellohello hellohellohellohellohello hello hello hello hello hello hello hello hello hello hello hello hello hellohellohello hello hello hello"
    }


    cell.setNeedsUpdateConstraints()
    cell.updateConstraintsIfNeeded()

    return cell
}

TableViewCell.swift

var label : UILabel!
var didUpdateConstraints = false

override init(style: UITableViewCellStyle, reuseIdentifier: String) {

    self.label = UILabel()
    self.label.textColor     = self.label.tintColor
    self.label.numberOfLines = 0

    self.label.setTranslatesAutoresizingMaskIntoConstraints(false)

    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.contentView.addSubview(self.label)
}

override func updateConstraints() {
    if !self.didUpdateConstraints {

        self.contentView.addConstraint(NSLayoutConstraint(item: self.label, attribute: .Leading, relatedBy: .Equal, toItem: self.contentView, attribute: .Leading, multiplier: 1, constant: 20))

        self.contentView.addConstraint(NSLayoutConstraint(item: self.label, attribute: .Trailing, relatedBy: .Equal, toItem: self.contentView, attribute: .Trailing, multiplier: 1, constant: 20))

        self.contentView.addConstraint(NSLayoutConstraint(item: self.label, attribute: .Bottom, relatedBy: .Equal, toItem: self.contentView, attribute: .Bottom, multiplier: 1, constant: 10))

        self.contentView.addConstraint(NSLayoutConstraint(item: self.label, attribute: .Top, relatedBy: .Equal, toItem: self.contentView, attribute: .Top, multiplier: 1, constant: 13))


        self.didUpdateConstraints = true
    }

    super.updateConstraints()
}

这是我的故事板: storyboard 我认为这可能是beta 4中的一个错误,但根据发布说明,它应该在beta 5中得到修复:
在beta 5中修复了: 当多行标签的宽度由于某些视图的layoutMargins更改而更改时,标签的内在内容大小没有被无效化,而应该被无效化。结果,布局可能会意外地截断标签(或文本视图)。///解决方法:正在更改layoutMargins的视图应覆盖layoutMarginsDidChange并将invalidateIntrinsicContentSize发送到标签。

尝试在您的viewWillAppear()中添加self.tableView.estimatedRowHeight = 200和self.tableView.rowHeight = UITableViewAutomaticDimension。 - Eddwin Paz
2个回答

0

您正在通过约束为您的表视图添加非常大的边距,这是距离边缘的距离。如果将此距离设置为 8 或 标准,则此距离将消失。如果仍然存在TableViewCell 高度问题,请参考以下代码:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let label = // Get the label from the cell here
    return label.frame.size.height + 16 // Assuming you have 2 constraints on top & bottom, each 8
}

0
你需要在“TableViewController”中添加以下行:
class TableViewController: UITableViewController

    override func viewDidLoad() {
        super.viewDidLoad()

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

设置单元格文本标签(多行和自动换行):
...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell                
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
}
...

移除不再需要的函数:

...
func override tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

}
...

这可以自动适配我的多行表格视图单元的高度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接