【问题标题】:Different cell height at runtime than in storyboard XCode9运行时的单元格高度与情节提要 XCode9 中的不同
【发布时间】:2018-02-13 06:59:59
【问题描述】:

我有一个单元格,我已经为其创建了一个子类,并且正在设置情节提要中的所有布局内容。在运行时,单元格比我在情节提要中设置的要短得多。

我在模拟器中获得了不同的背景颜色和高度。

class FeedCell: UITableViewCell {
@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var messageLabel: UILabel!

func configureCell(profileImage : UIImage, email : String, content : String) {
    self.profileImage.image = profileImage
    self.emailLabel.text = email
    self.messageLabel.text = content
}
override func awakeFromNib() {
    super.awakeFromNib()
}
 }

这是我的子类中唯一的代码。

有什么想法吗?

【问题讨论】:

  • 您有约束问题。请向我们展示您的限制条件。

标签: ios swift uitableview storyboard


【解决方案1】:

使用estimatedRowHeightrowHeight 为表格单元格设置适当的约束并设置表格视图动态高度

例如

    tableView.estimatedRowHeight = 120
    tableView.rowHeight = UITableViewAutomaticDimension

【讨论】:

    【解决方案2】:

    当您创建自定义大小的表格视图单元格(动态行高)时,使用适当的约束来定义单元格的大小并在里面设置以下属性(rowHeightestimatedRowHeightviewDidLoad 方法。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.yourTableView.rowHeight = UITableViewAutomaticDimension
        self.yourTableView.estimatedRowHeight = 130
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2012-11-19
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      相关资源
      最近更新 更多