Ios 如何使用uistackview根据自定义xib高度设置动态UITableViewCell高度
我无法将UITableViewcell设置为具有动态高度 我有一个自定义的xib,结构如下, 并已将堆栈视图的前导、尾随和顶部约束添加到其superview } 在tableView cellForRowAt中,我创建了自定义视图并将其添加到每个UITableViewCell中Ios 如何使用uistackview根据自定义xib高度设置动态UITableViewCell高度,ios,swift,uitableview,stack,uistackview,Ios,Swift,Uitableview,Stack,Uistackview,我无法将UITableViewcell设置为具有动态高度 我有一个自定义的xib,结构如下, 并已将堆栈视图的前导、尾随和顶部约束添加到其superview } 在tableView cellForRowAt中,我创建了自定义视图并将其添加到每个UITableViewCell中 cell.contentView.addSubview(fieldView) NSLayoutConstraint(item: fieldView, attribute: NSLayoutConstraint.Att
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
}
}
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
}
使用以下命令:
let lineCount = YourLabel. calculateMaxLines
现在设置标签每行的高度
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
}
}
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()
当textfield内容更改时调用什么是“cell.contentView.addSubview(fieldView)”?将自定义xib视图添加到TableViewCell您需要显示如何“创建自定义视图并将其添加到每个
UITableViewCell
”——很可能是此时未正确设置约束。