ios - 在 Swift 3 中动态调整 UITableViewCell 的高度失败

标签 ios swift uitableview uitableviewautomaticdimension

我有一个自定义的 UITableViewCell,我希望它根据我的标签文本内容更新高度。
但是我尝试在我的 ViewController ViewDidLoad 中使用这段代码:

  tableView.register(ChatRightTextTableViewCell.self, forCellReuseIdentifier: ChatRightTextTableViewCell.identifier)
  tableView.estimatedRowHeight = 100.0
  tableView.rowHeight = UITableViewAutomaticDimension


它似乎没有更新高度。
我的单元格限制有什么问题?
谢谢。

![cell constrain wrong like this.

import snapKit
import UIKit

class ChatRightTextTableViewCell: UITableViewCell {

static let identifier = "ChatRightTextTableViewCell"
var cellHeight: CGFloat = 0

var labelContent:UILabel = { ()->UILabel in
    let ui:UILabel = GeneratorLabel()
    ui.textColor = UIColor(red:0.20, green:0.20, blue:0.20, alpha:1.00)
    ui.backgroundColor = UIColor.white
    ui.font = defaultTextFont
    ui.numberOfLines = 0
    ui.lineBreakMode = NSLineBreakMode.byCharWrapping
    ui.layer.cornerRadius = defaultButtonRadius
    ui.layer.masksToBounds = true
    return ui
}()

var labelDatetime:UILabel = { ()->UILabel in
    let ui = GeneratorLabel()
    ui.font = defaultMessageTimeFont
    ui.textColor = defaultChatTimeColor
    ui.numberOfLines = 0
    return ui
}()

var icon:UIImageView = { ()->UIImageView in
    let ui = GeneratorAvatarImageView()
    ui.isUserInteractionEnabled = true
    return ui
}()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    loadContent()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    loadVFL()
}

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


func loadContent() {
    backgroundColor = defaultBackgroundColor
    selectionStyle = .none
    contentView.addSubview(labelContent)
    contentView.addSubview(labelDatetime)
    contentView.addSubview(icon)
}

func loadVFL() {

    let datetimeHeight:CGFloat = UIScreen.main.bounds.height*0.0195 //13

    let contentWidth:CGFloat = contentView.bounds.width*0.62  //254.5
    let iconLeftPadding:CGFloat = contentView.bounds.width*0.0266 //15
    let contentAndDatePadding:CGFloat = UIScreen.main.bounds.height*0.0089 //6
    let dateAndBottomPaddding:CGFloat = UIScreen.main.bounds.height*0.0090 //6
    let topPadding:CGFloat            = UIScreen.main.bounds.height*0.0149 //10
    let nameLabelBottomPadding:CGFloat = UIScreen.main.bounds.height*0.0075 //5

    let views = DictionaryOfInstanceVariables(self, objects: "labelContent","labelDatetime","icon")
    let metrics = ["padding":iconLeftPadding,"contentAndDatePadding":contentAndDatePadding,"dateAndBottomPaddding":dateAndBottomPaddding,"nameLabelBottomPadding":nameLabelBottomPadding,"topPadding":topPadding]

    labelContent.frame = CGRect(x: 0, y: 0, width: contentWidth, height: CGFloat.greatestFiniteMagnitude)
    labelContent.sizeToFit()

    icon.snp.makeConstraints { (make) -> Void in
        make.width.equalTo(defaultChatroomIconWidth)
        make.height.equalTo(defaultChatroomIconWidth)
        make.centerY.equalToSuperview()
    }

    labelDatetime.snp.makeConstraints { (make) in
        make.height.equalTo(datetimeHeight)
    }

    labelContent.backgroundColor = UIColor.red
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[labelContent]-padding-[icon]-padding-|", options: [], metrics: metrics, views: views))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[labelDatetime]-padding-[icon]-padding-|", options: [], metrics: metrics, views: views))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-topPadding-[labelContent]-contentAndDatePadding-[labelDatetime]-dateAndBottomPaddding-|", options: [], metrics: metrics, views: views))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-topPadding-[icon]", options: [], metrics: metrics, views: views))
}
}

最佳答案

捕获了,

如果你使用了 xib,你会注意到这个警告,因为你已经对它进行了编码并放置了很难猜出问题的标签 :)

您正在使用两个标签,它们基本上根据它们显示的内容决定单元格的高度。但是因为两者在纵轴上都具有 Same Content Hugging 优先级,所以仅对这些标签提供顶部和底部约束是不够的。

您必须将垂直轴上的内容拥抱优先级设置为标签之一(您认为显然内容会更小)到更高的值,以提供足够的数据来计算单元格的动态高度。

总结:

简单设置

labelDatetime.setContentHuggingPriority(252, for: .vertical)

var labelDatetime:UILabel = { ()->UILabel in
let ui = GeneratorLabel()
ui.font = defaultMessageTimeFont
ui.textColor = defaultChatTimeColor
ui.numberOfLines = 0
ui..setContentHuggingPriority(252, for: .vertical)
return ui
}()

关于ios - 在 Swift 3 中动态调整 UITableViewCell 的高度失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45005077/

相关文章:

ios - Iphone App 拒绝 Ipv6 不兼容

ios - heightForRowAtIndexPath 间歇性崩溃

uitableview - 静态 UITableViewCell 中的 UISwitch 生成错误

ios - UITableView 内容大小不正确

iphone - GAITrackedViewController 和 UITableViewController

ios - 在 Firebase iOS 中刷新身份验证 token 时出错

iOS自定义选项卡式应用方法

ios - 在 Main.Storyboard 中显示所有 View Controller

ios - 如何在 ios 中以编程方式设置 UICollectionView 的单元格大小

xcode - QLPreviewView 在设置 previewItem 时在运行时导致 EXC_BAD_ACCESS 错误