动态调整自定义xib单元的大小

问题描述 投票:0回答:1

所以我在使用自定义xib单元时遇到了一些麻烦,无法根据单元内部的UILabel动态调整大小。我已经设置了

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 250
    }

XIB单元ViewController

import UIKit

class ReminderTableViewCell: UITableViewCell {

@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var notesLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    backgroundColor = .clear // very important
    layer.masksToBounds = false
    layer.shadowOpacity = 1
    layer.shadowRadius = 4

    layer.shadowOffset = CGSize(width: 2 , height: 2)
    layer.shadowColor = UIColor.darkGray.cgColor

    // add corner radius on `contentView`
    contentView.backgroundColor = .darkGray
    contentView.layer.cornerRadius = 8
    progressBar.transform = progressBar.transform.scaledBy(x: 1, y: 5)
    progressBar.layer.cornerRadius = 15
    progressBar.clipsToBounds = true
    self.layer.sublayers![1].cornerRadius = 15
    self.subviews[1].clipsToBounds = true
}

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


    // Configure the view for the selected state
}


func setInfo(title:String, notes:String,date:String){
    titleLabel.text = title
    notesLabel.text = notes
    dateLabel.text = date
}


override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 15, bottom: 15, right: 10))
}

 }

XIB上的约束xib constraints

看起来像什么Look

我将titleLabel和userNotes的行都设置为0,并且都将自动换行。

如果你们能帮助我,我会非常感谢。

swift xcode uitableview xib
1个回答
0
投票

一旦您应该尝试这样。

func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath)-> CGFloat {返回250}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
© www.soinside.com 2019 - 2024. All rights reserved.