所以我在使用自定义xib单元时遇到了麻烦,无法根据单元内部的UILabel动态调整大小。我已经设定
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
XIB Cell 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的限制
看起来像什么
我将titleLabel和userNotes的行都设置为0,并且都将自动换行。
如果你们能帮助我,我将非常感谢。
最佳答案
一旦你应该尝试这样。
func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath)-> CGFloat {
返回250
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
关于swift - 动态调整自定义Xib单元的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58350913/