在iOS Swift中以编程方式创buildUITableView(表格高度不能自动resize)

我知道有很多类似的问题。 但是,我的问题没有一个正确的答案。 我需要以编程方式创build自定义UITableView – 根本不使用拖放。 只需swift代码。 我已经做了几乎所有事情,除了一件事情没有成功。

自定义TableView单元格看起来像这样

--------------------------------- ------- | Pic | Name | | Profession ------- Multi-line text here (title) Multi-line text here again that will have more description of what is written above List of all phone numbers, multi-line again - multiple phone numbers footer ---------------------------------- 

除了表格单元格高度的自动resize不工作,我已经成功了一切。 我想完成这个程序化,所以面临的问题。 另外,我没有太多的iOS编程经验。

这是我读了很多stackoverflow的答案和其他来源后的模式。

模型

 class Model { // members init(..) { // members initialized } } 

CustomCell

 class CustomCell: UITableViewCell { // cell members, fields override init(style: UITableViewCellStyle, reuseIdentifier: String?) { // all fields initialized } override func prepareForReuse() { super.prepareForReuse() } override func layoutSubviews() { super.layoutSubviews() self.updateConstraints() } func autoAdjustTextView(textView: UITextView) { textView.scrollEnabled = false // this is important let fixedWidth = textView.frame.size.width textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max)) let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max)) var newFrame = textView.frame newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height) textView.frame = newFrame } override func updateConstraints() { super.updateConstraints() // set AutoLayout constraints using Cartography autoAdjustTextView(descriptionMultiLineText) autoAdjustTextView(titleMultiLineText) autoAdjustTextView(phoneNumberMultiLineText) constrain(background) { background in background.top == background.superview!.top background.left == background.superview!.left background.right == background.superview!.right // background.height == CELL_HEIGHT } constrain(background, iconContainerView) { background, iconContainerView in iconContainerView.left == background.left + CustomSizes.PADDING iconContainerView.top == background.top + CustomSizes.PADDING } constrain(background, textIconLabel, userNameLabel) { background, imageLabel, nameLabel in nameLabel.left == imageLabel.right + CustomSizes.PADDING nameLabel.top == imageLabel.top } constrain(background, userNameLabel, professionLabel) { background, nameLabel, label in label.left == nameLabel.left label.top == nameLabel.bottom + CustomSizes.PADDING_SMALL } constrain(background, professionLabel, updatedAtLabel) { background, professionLabel, label in label.left == professionLabel.right + CustomSizes.PADDING label.top == professionLabel.top } constrain(titleMultiLineText, background, professionLabel) { titleMultiLineText, background, professionLabel in titleMultiLineText.left == background.left + CustomSizes.PADDING titleMultiLineText.right == background.right - CustomSizes.PADDING titleMultiLineText.top == professionLabel.bottom + CustomSizes.PADDING_SMALL } constrain(descriptionMultiLineText, background, titleMultiLineText) { descriptionMultiLineText, background, titleMultiLineText in descriptionMultiLineText.left == background.left + CustomSizes.PADDING descriptionMultiLineText.right == background.right - CustomSizes.PADDING descriptionMultiLineText.top == titleMultiLineText.bottom } constrain(phoneNumberMultiLineText, background, descriptionMultiLineText) { phoneNumberMultiLineText, background, descriptionMultiLineText in phoneNumberMultiLineText.left == background.left + CustomSizes.PADDING phoneNumberMultiLineText.right == background.right - CustomSizes.PADDING phoneNumberMultiLineText.top == descriptionMultiLineText.bottom } // more constraints for some icons on a single line below the phone number multi line field didSetupConstraints = true } } 

视图控制器

 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // members and variables init() { super.init(nibName: nil, bundle: nil) print("init()") } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() // show loading self.setUpView(true) } func setUpView(isFirstTime: Bool) { // load data from network if isFirstTime { self.setupTableView() self.addConstraints() } else { self.tableView.reloadData() } } func setupTableView() { self.tableView = UITableView(frame: view.bounds, style: .Grouped) tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .None tableView.registerClass(CustomCell.self, forCellReuseIdentifier: NSStringFromClass(CustomCell)) tableView.scrollEnabled = true tableView.estimatedRowHeight = 200 tableView.rowHeight = UITableViewAutomaticDimension self.view.addSubview(tableView) } func addConstraintsForView() { constrain(self.view, tableView) { view, tableView in tableView.top == view.top tableView.left == view.left tableView.right == view.right tableView.bottom == view.bottom } } // other tableView methods implemented func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.All } } 

我现在应该怎么做 ? 或者有没有更好的方法?