我无法将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也没有发生变化
我该如何解决?
答案 0 :(得分:0)
最后我弄清楚了这个问题
只需添加这两行
self.tableView.beginUpdates()
self.tableView.endUpdates()
在文本字段内容更改时调用
答案 1 :(得分:-1)
首先,您必须计算UILabel的行数
printf "(H) heads or (T) tails"
read user_choice
if [ $user_choice != H ] && [ $user_choice != T ]; then
echo invalid choice defaulting to heads
user_choice=H=1
user_choice=T=0
fi
#value of 1 is heads, 2 is Tails
computer_choice=$(($RANDOM% 2 + 1))
if [ $computer_choice == 1 ]; then
echo computer chooses tails
fi
if [ $computer_choice == 1 ] && [ $user_choice = H ]; then
echo you win!
else
echo you lose
if [ $computer_choice == 2 ] && [ $user_choice = T ]; then
echo you win!
echo computer chooses heads
else
echo you lose
fi
fi
否则,您可以扩展以计算标签的最大行数
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