ios - 编码的自动布局对于 UITableView 无法正常工作

标签 ios swift uitableview autolayout

我正在使用 UITableView 处理提要页面,并且我正在尝试设置单元格的动态高度(以根据内容进行调整。)

我已经为单元格创建了一个自定义类,代码如下:

class cellTableViewCell: UITableViewCell, UINavigationControllerDelegate {



//OUTLETS
@IBOutlet weak var usernameLbl: UILabel!
@IBOutlet var profileImg: UIImageView!



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code



    usernameLbl.text = "Username"
    usernameLbl.textColor = UIColor.grayColor()

    profileImg.layer.cornerRadius = profileImg.frame.size.width/2
    profileImg.layer.masksToBounds = true


    profileImg.addConstraint(NSLayoutConstraint(item: profileImg, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 100.0))

    contentView.addSubview(profileImg)

    contentView.addConstraint(NSLayoutConstraint(item: profileImg, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 100.0))




}

然后,在控制 TableView 的 View Controller 的 ViewDidLoad() 方法中,我添加了以下内容:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 160.0

但是,单元格根本没有适应,因为高度仍然小于内容。

最佳答案

我认为您的约束是完全正确的,但问题是您无法使用每个单元格具有不同高度的动态 TableViewCells 构建 UITableView。我自己尝试过,但在动态 UITableViewCell 中,tableView 的属性 rowHeight (tableView.rowHeight) 。因此,此 rowHeight 将覆盖由约束定义的单元格的每个不同高度。

关于ios - 编码的自动布局对于 UITableView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32071364/

相关文章:

android - cordova 蓝牙音箱没有声音

ios - 仅适用于 iOS 10 的 Swift 3 中的 NSBatchDeleteRequest?

objective-c - 从 TableView 中删除行

xcode - XCode- objective-c :删除表行崩溃

ios - 将 TableCell 插入正确的部分

ios - 删除 WithCompletionHandler 导致退出游戏中心

ios - 如何为UINavigation栏添加阴影效果

ios - 支持 iOS 12 和 13 时的 AppDelegate 和 SceneDelegate

ios - 提醒复选框未在 swift 中第一次选中

ios - 我如何在 swift 3 的一个 View Controller 中填充两个不同的 TableView