【问题标题】:Multiline UILabel within a Static UITableViewCell on iOS 9iOS 9 上的静态 UITableViewCell 中的多行 UILabel
【发布时间】:2015-09-14 05:54:12
【问题描述】:

我正在尝试在静态 UITableViewCell 多行中创建一个 UILabel,具体取决于它的内容,它会发生变化。

viewDidLoad 方法中,我有初始化标签内文本的代码,紧随其后的是:

cellLabel.text = "Here is some text, but because of how long it is, it has to span a number of lines."  
cellLabel.numberOfLines = 0  
cellLabel.sizeToFit()  
tableViewCell.textLabel?.sizeToFit()  
tableViewCell.textLabel?.numberOfLines = 0  
tableViewCell.textLabel?.lineBreakMode = .ByWordWrapping

然后我正在尝试计算该行必须达到的高度以容纳多行标签:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {  
        if (indexPath.section == 2 && indexPath.row == 0) {  
            return cellLabel.frame.height  
        }  
        else {  
            return 44  
        }  
}

然而,返回的是一个多行 UITableViewCell 但文本的结尾被截断并且不显示。我会在 return definitionLabel.frame.height 的末尾添加一个值,但是标签的内容会有所不同。

有人可以告诉我如何使用 Swift 2.0 和 iOS 9 在静态 UITableViewCell 中简单地拥有一个多行 UILabel。

注意:我已添加:

tableView.rowHeight = UITableViewAutomaticDimension

我正在使用一个基本的静态表格视图单元格,它默认应用了约束。

【问题讨论】:

  • 与单元格的动态高度相同:tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 160.0
  • 在不处理单元高度的情况下出售:raywenderlich.com/87975/…
  • 我已经添加了 tableView.rowHeight = UITableVieeAutomaticDimension 并且没有运气。我有一个静态表格视图,所以只是使用一个具有自己约束的基本单元格。
  • 你正在运行raywenderlich.com/87975/…的演示
  • 你有解决办法吗?我也面临同样的挑战。我在静态表格单元格中有一个标签,我希望它是多行和自动高度。我已经尝试过tableView.rowHeight = UITableViewAutomaticDimensionoverride func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat。但就像你的一样,文本被截断了。

标签: ios swift uitableview swift2


【解决方案1】:

在故事板上选择单元格并将自定义行高设置为 0。从 iOS 9 开始,这对我有用。

http://i.stack.imgur.com/WmjPX.png

这是 Xcode 7 中的一个错误,它无论如何都会在 xib xml 中设置单元格的高度,这在以前的 Xcode 中也是一个问题,但没有那么严重。当您正在处理的情节提要位于前台时,就会发生这种刷新。

在以前版本的 Xcode 中,我不得不手动删除 xib 文件编辑中的 tableviewcell rect 属性,一切都会好起来的,但最新版本一直搞砸了。

<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" rowHeight="0.0" id="uWP-2k-uRh">
    <rect key="frame" x="0.0" y="64" width="600" height="XX.X"/> <-- Xcode 7 keeps adding this line here

那么你需要的 viewDidLoad 就是

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.estimatedRowHeight = 200.0
    tableView.rowHeight = UITableViewAutomaticDimension
}

感谢苹果...

【讨论】:

  • 我认为你做错了什么。对我来说开箱即用。
  • 这对我有用。我已经尝试了其他所有方法,包括按照此处的建议删除情节提要中的“rect”属性:stackoverflow.com/questions/31979932/…我的项目是 Objective-C 而不是 Swift,但是将自定义行高设置为 0 效果很好。
  • 从 Xcode 9 开始,取消选中“自定义”(而不是将高度设置为 0)也可以。但是,这会将 Interface Builder 上的单元格高度重置为 44(仍然比零好)。
【解决方案2】:

我已经尝试了所有可用的解决方案,从覆盖drawRect 到调用layoutSubviews,但我想我只是插嘴说也许可以尝试再次重做那个表格视图单元格。

我刚刚做了,但我很困惑它终于可以开箱即用了。我尝试将以前的 XML 代码与新的 XML 代码进行比较,我看到的唯一明显的是在 &lt;/prototypes&gt; 之后的末尾添加了一个 &lt;section/&gt; 标记。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多