[英]Xcode - Swift - UILabel in TableViewCell Expansion
I have a UILabel in a custom UITableViewCell called "ScheduleCell", like so: 我在名为“ ScheduleCell”的自定义UITableViewCell中有一个UILabel,如下所示:
Here is the code for ScheduleCell: 这是ScheduleCell的代码:
import UIKit
class ScheduleCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I want the tileLabel to expand based on its text and I want the ScheduleCell to expand with it. 我希望tileLabel根据其文本进行扩展,并且希望ScheduleCell随其进行扩展。 Here are the attributes of the titleLabel: 这是titleLabel的属性:
Here are the attributes of the cell prototype, which is linked to the ScheduleCell class: 这是单元原型的属性,链接到ScheduleCell类:
Here is some relevant code for the ViewController: 这是ViewController的一些相关代码:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160.0
And 和
func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ScheduleCell
cell.backgroundColor = UIColor(rgb: 0xF6FBFE)
cell.titleLabel.text = (Globals.scheduleArr[indexPath.row][3] as! String)
cell.titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
return cell
}
All of the titles from "Globals.scheduleArr" fit on one line except one, which is "Jane Richards Grey Reads "To Kill a Mockingbird"". “ Globals.scheduleArr”中的所有标题都只适合一行,唯一的一行是“ Jane Richards Gray读“杀死一只知更鸟””。 Here is what the ViewController looks like in the iOS Simulator: 这是iOS模拟器中ViewController的外观:
As you can see, the label does not expand as necessary. 如您所见,标签不会根据需要展开。 Strangely enough, when I add a "\\n" to the end of each of the titles, the label does expand. 奇怪的是,当我在每个标题的末尾添加“ \\ n”时,标签确实会扩展。 However, the cell doesn't seem to expand with the label: 但是,该单元格似乎没有随标签扩展:
Any help in resolving this issue would be greatly appreciated! 解决此问题的任何帮助将不胜感激!
If you want to use UITableViewAutomaticDimension, you need to add Label-ContentView bottom constraint . 如果要使用UITableViewAutomaticDimension,则需要添加Label-ContentView 底部约束 。
Add new bottom constraint and try again! 添加新的底部约束,然后重试!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.