ios - 单行文本在 UILabel 中占用两行

标签 ios uitableview swift uilabel

enter image description here

正如您在图片中看到的那样,中间的单元格有一个占用两行的 UILabel,但文本实际上是一行。似乎如果文本只需要几个字符来创建一个新行,iOS 就会假定它已经有 2 行。这很奇怪。

这是我创建标签的方式:

self.titleLabel.lineBreakMode = .ByTruncatingTail
self.titleLabel.numberOfLines = 0
self.titleLabel.textAlignment = .Left

约束设置一次:

self.titleLabel.autoPinEdgeToSuperviewEdge(.Top)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Leading)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Trailing)
self.titleLabel.autoPinEdgeToSuperviewEdge(.Bottom)

奇怪的是,当表格滚动时,奇数单元格消失并再次滚动回来时,它具有正常高度。滚动后:

enter image description here

任何想法出了什么问题?我正在使用 swift、xcode6.1 和 iOS8.1

表格 View Controller :

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(CityTableViewCell.self, forCellReuseIdentifier:"cell")
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 52
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let cell: CityTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as? CityTableViewCell {
        // Configure the cell for this indexPath
        cell.updateFonts()
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        if indexPath.row == 1 {
            cell.titleLabel.text = "Welcome to city 17, Mr. Gordon F."
        } else {
            cell.titleLabel.text = "Lamar!!!"
        }
        // Make sure the constraints have been added to this cell, since it may have just been created from scratch
        cell.setNeedsUpdateConstraints()
        cell.updateConstraintsIfNeeded()

        return cell
    }
    return UITableViewCell();
}

最佳答案

我想你遇到了这个错误:http://openradar.appspot.com/17799811 .标签未正确设置 preferredMaxLayoutWidth

我选择的解决方法是将 UITableViewCell 子类化为以下类:

class VFTableViewCell : UITableViewCell {

    @IBOutlet weak var testoLbl: UILabel!

    //MARK: codice temporaneo per bug http://openradar.appspot.com/17799811

    func maxWidth() -> CGFloat {

        var appMax = CGRectGetWidth(UIApplication.sharedApplication().keyWindow.frame)

        appMax -= 12 + 12    // borders, this is up to you (and should not be hardcoded here)

        return appMax
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        // MARK: Required for self-sizing cells.
        self.testoLbl.preferredMaxLayoutWidth = maxWidth()
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // MARK: Required for self-sizing cells
        self.testoLbl.preferredMaxLayoutWidth = maxWidth()
    }
}

操作说明:

自动布局似乎没有正确计算 UILabel 布局的宽度。在我的 UITableViewCell 子类中将首选宽度设置为父宽度解决了我的问题:

self.titleLabel.preferredMaxLayoutWidth = self.frame.width

在 SO 上找到:https://stackoverflow.com/a/19777242/401025

关于ios - 单行文本在 UILabel 中占用两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840137/

相关文章:

ios - 如何在CGrect中制作尖锐的形状边界?

ios - 将历史数组值插入 UITableViewController

swift - 在 2 个 TableView 中使用结构体(一个 TableView 在另一个 TableView 中)

ios - 在 swift 中将边框宽度和颜色分配给 textview 时应用程序崩溃

swift - 将 RxSwift 计时器和间隔可观察量组合成一个带有完成的可观察量

ios - Swift - 如何在 UIAlertController 中放置页面 View ?

具有支付处理功能的 iOS 刷卡器

ios - 使用给定 key 在 iOS 中使用 AES128 CTR 加密和解密随机 NSString

ios - 如何将添加文本框合并到 navigationItem -> rightBarButtonItem?

iOS Textfield/select 组合过滤大量数据