如何使用 uistackview 根据自定义 xib 高度设置动态 uitablviewcell 高度

How to set dynamic uitablviewcell height according to a custom xib height with uistackview

我无法将 UITableViewcell 设置为具有动态高度。

我有一个自定义的 xib 和如下结构, 并且已经将前导、尾随和顶部约束添加到其父视图的堆栈视图

class fieldView: UIView {
let kCONTENT_XIB_NAME = "form_field_view"

@IBOutlet weak var contectView: UIView!
@IBOutlet weak var fieldDesc: UILabel!
@IBOutlet weak var fieldInput: customTextField!

var required: Bool?

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()

}

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

func commonInit() {
    Bundle.main.loadNibNamed(kCONTENT_XIB_NAME, owner: self, options: nil)
    addSubview(contectView)
    contectView.frame = self.bounds
    contectView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    fieldInput.isScrollEnabled = false
}

}

在 tableView cellForRowAt 中,我创建了自定义视图并将其添加到每个 UITableViewCell

cell.contentView.addSubview(fieldView)
NSLayoutConstraint(item: fieldView, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.leadingMargin, multiplier: 1.0, constant: 10.0).isActive = true
        NSLayoutConstraint(item: fieldView, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.trailingMargin, multiplier: 1.0, constant: 100.0).isActive = true
        NSLayoutConstraint(item: fieldView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.topMargin, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: fieldView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: cell.contentView, attribute: NSLayoutConstraint.Attribute.bottomMargin, multiplier: 1.0, constant: 0.0).isActive = true
return cell

对于 tableView

override func viewDidLoad() {
    super.viewDidLoad()

    fieldTable.rowHeight = UITableView.automaticDimension
    fieldTable.estimatedRowHeight = 200.0
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (expanded) {
        return (UITableView.automaticDimension)
    }else{
        return 0
    }
}

然而实际结果是这样的

我认为 UIStackView 改变了它的高度,但它的 superview 没有改变,这使得 tableviewcell 也没有改变

我该如何解决这个问题?

首先你要统计UILabel的行数

 NSInteger lineCount = 0;
 CGSize textSize = CGSizeMake(yourLabel.frame.size.width, MAXFLOAT);
 int rHeight = lroundf([yourLabel sizeThatFits:textSize].height);
 int charSize = lroundf(yourLabel.font.lineHeight);
 lineCount = rHeight/charSize;
 NSLog(@"No of lines: %i",lineCount);

否则你可以使用扩展来计算标签的最大行数

extension UILabel {
    func calculateMaxLines() -> Int {
        let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity))
        let charSize = font.lineHeight
        let text = (self.text ?? "") as NSString
        let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        let linesRoundedUp = Int(ceil(textSize.height/charSize)) 
        return linesRoundedUp
    }    
}

使用那个:

let lineCount = YourLabel. calculateMaxLines 

现在设置每行标签的高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return lineCount * 18 //If total 3 line so 3*18 = 54 is height else you can take any number instead of 18 according to font size height
}

这道题我终于想通了

只需添加这两行

self.tableView.beginUpdates()
self.tableView.endUpdates()

文本字段内容更改时调用