ios - 自调整 TableView 单元格不起作用

标签 ios swift uitableview

我将 estimatedRowHeight 设置为常量并将 rowHeight 设置为 UITableViewAutomaticDimension 但表格 View 单元格仍未调整到我的单元格的 detailTextLabel .我哪里错了?

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.estimatedRowHeight = 130.0
    tableView.tableFooterView = UIView()
    tableView.separatorInset.left = 50
    tableView.registerClass(CommentCellView.self, forCellReuseIdentifier: cellid)
    tableView.rowHeight = UITableViewAutomaticDimension


}

override func viewDidAppear(animated: Bool) {
    tableView.reloadData()
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellid, forIndexPath: indexPath) as! CommentCellView

    return cell
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5

}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 60.0
}

UITableViewCell 类:

class CommentCellView: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)

        detailTextLabel?.numberOfLines = 0
        detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

}

enter image description here

最佳答案

设置你的标签(在本例中称为yourText)

 var yourText : UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

以这种方式设置单元格约束并将 setupComponents 方法放在这里:

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

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: "CellId")

        setupComponents()

    }

        func setupComponents(){



    self.addSubview(yourText)

    yourText.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 6).active = true
            yourText.centerYAnchor.constraintEqualToAnchor(self.centerYAnchor).active = true
            yourText.widthAnchor.constraintEqualToConstant(110).active = true

    yourText.numberOfLines = 0

    yourText.lineBreakMode = NSLineBreakMode.ByWordWrapping
     self.bottomAnchor.constraintEqualToAnchor(yourText.bottomAnchor, constant: 5).active = true
    }

关于ios - 自调整 TableView 单元格不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39440504/

相关文章:

ios - AGX : Texture read/write assertion failed: height > 0

ios - Azure 语音转文本 API

ios - 如何在 Swift 中获取自定义单元格中值组件的索引路径?

ios - UITableView 的 headerview 中的 UISwitch 正在更改状态但不调用事件处理方法

iphone - 我可以支持 iOS 3.0 到 6.0 版本吗?

ios - 使用 swift 在文本标签中的文本之间嵌入一个图标

所选类实例的 Swift 扩展

ios - 如何在 Swift iOS 中关闭上一个 View Controller 并移至下一个 View Controller

ios - 为什么我的项目中出现三个 "Illegal Configuration"错误?

ios 7 UIView animateWithDuration : . 。立即运行