繁体   English   中英

Xcode-Swift-TableViewCell扩展中的UILabel

[英]Xcode - Swift - UILabel in TableViewCell Expansion

我在名为“ ScheduleCell”的自定义UITableViewCell中有一个UILabel,如下所示:

在此处输入图片说明

这是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
}

}

我希望tileLabel根据其文本进行扩展,并且希望ScheduleCell随其进行扩展。 这是titleLabel的属性:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

这是单元原型的属性,链接到ScheduleCell类:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

这是ViewController的一些相关代码:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160.0

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
}

“ Globals.scheduleArr”中的所有标题都只适合一行,唯一的一行是“ Jane Richards Gray读“杀死一只知更鸟””。 这是iOS模拟器中ViewController的外观:

在此处输入图片说明

如您所见,标签不会根据需要展开。 奇怪的是,当我在每个标题的末尾添加“ \\ n”时,标签确实会扩展。 但是,该单元格似乎没有随标签扩展:

在此处输入图片说明

解决此问题的任何帮助将不胜感激!

如果要使用UITableViewAutomaticDimension,则需要添加Label-ContentView 底部约束

添加新的底部约束,然后重试!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM