具有大小类的自调整 UITableViewCells 中的多行 UILabel - iOS 8 和 Swift

Posted

技术标签:

【中文标题】具有大小类的自调整 UITableViewCells 中的多行 UILabel - iOS 8 和 Swift【英文标题】:Multiline UILabel in self-sizing UITableViewCells with size-classes - iOS 8 & Swift 【发布时间】:2014-10-01 01:07:41 【问题描述】:

我在使用 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()

这是我的故事板:-

我认为这是 beta 4 中的一个错误,但正如发行说明所说,它应该在 beta 5 中修复:

在 beta 5 中修复: 当多行标签的宽度由于某些视图的 layoutMargins 变化而发生变化时,标签的内在内容大小不会在应该失效的时候失效。因此,布局可能会意外截断标签(或文本视图)。 /// 解决方法:layoutMargins 发生变化的视图应该覆盖 layoutMarginsDidChange 并将 invalidateIntrinsicContentSize 发送到标签。

【问题讨论】:

尝试将 self.tableView.estimatedRowHeight = 200 self.tableView.rowHeight = UITableViewAutomaticDimension 添加到您的 viewWillAppear() 【参考方案1】:

您通过约束在表格视图上添加了相当大的边距,这是到边缘的距离。如果将此距离设置为 8 或 Standard,则此距离将消失。这应该也可以解决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

【讨论】:

【参考方案2】:

您必须将以下行添加到“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 


...

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

【讨论】:

以上是关于具有大小类的自调整 UITableViewCells 中的多行 UILabel - iOS 8 和 Swift的主要内容,如果未能解决你的问题,请参考以下文章

如何调整 UIImageView 的大小并强制重新布局父 UITableViewCell

关于单元重用的自定义 UITableViewCell 约束问题

在具有自动布局的 UITableViewCell 中动态调整大小的 UIWebView - 违反约束

TextField 子视图未通过 UITableViewCell 框架大小更改调整大小

如何在编辑模式下调整 UITableViewCell 背景的大小

具有 uitableviewdiffabledatasource 的自调整单元格