这是我要制作的 UITableView
:
为了实现这一点,我把cell的contentView
分成了三部分。
1 - fromView:包含开始时间和类(class)类型。高度将为 contentView
的 30%2 - infoView:包含时钟图像、类(class)名称和教授姓名。高度将为 contentView
的 40%。
3 - toView:包含结束时间和位置。高度将是剩下的空间 (30%)。
首先,在进入细节之前,我决定只展示这个容器。为了理解一个问题,我画了它们(分别为绿色、红色、蓝色)。
将它们添加到 contentView
后,以下是我对这些容器的约束:
fromView.easy.layout(Top(), Left(), Right(), Height(*0.3).like(contentView))
infoView.easy.layout(Top().to(fromView), Left(), Right(), Height(*0.4).like(contentView))
toView.easy.layout(Top().to(infoView), Left(), Right(), Bottom())
看来一切正常。启动后,我以为我会看到它们,但这是发生的事情:
我认为单元格不知道它需要多大,所以我决定实现以下功能:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
在此之后,UITableView
比以前更正确地显示了容器。但是,我计划将一些 UILabel
和其他 views
添加到该容器中(请看第一张图片)。换句话说,我不知道单元格需要多大。它需要是动态的。无论如何,我尝试将这些标签添加到容器中,也许在那之后,一个单元格会计算出高度。这是我的做法:
func setupViews() {
fromView.addSubviews(fromTime, classType)
toView.addSubviews(toTime, room)
infoView.addSubviews(clockImage, teacherName, subjectName)
contentView.addSubviews(toView, fromView, infoView)
}
func setupConstraints() {
fromView.easy.layout(Top(), Left(), Right(), Height(*0.3).like(contentView))
infoView.easy.layout(Top().to(fromView), Left(), Right(), Height(*0.4).like(contentView))
toView.easy.layout(Top().to(infoView), Left(), Right(), Bottom())
fromTime.easy.layout(CenterY(), Left(8))
fromTime.setContentHuggingPriority(.required, for: .horizontal)
fromTime.setContentCompressionResistancePriority(.required, for: .horizontal)
classType.easy.layout(CenterY(), Left(8).to(fromTime))
clockImage.easy.layout(CenterY(), Left(16), Width(24), Height(24))
subjectName.easy.layout(CenterY(-8), Left().to(classType, .left), Right())
teacherName.easy.layout(Top(8).to(subjectName), Left().to(subjectName, .left), Right())
toTime.easy.layout(CenterY(), Left(8))
toTime.setContentHuggingPriority(.required, for: .horizontal)
toTime.setContentCompressionResistancePriority(.required, for: .horizontal)
room.easy.layout(CenterY(), Left(8).to(toTime))
}
但无论如何,问题又出现了:
我想,问题是我根据 contentView
的高度给容器高度,但是 contentView
不知道它的高度。但我也不知道它需要多高,因为它需要根据标签的大小而定。那么,如何解决这个问题呢?
最佳答案
不要为容器设置高度。您必须确保单元格的顶部和底部通过约束连接。我的建议是去掉容器,将所有标签和图像直接添加到单元格中,然后像这样创建约束。
然后设置tableView.rowHeight = UITableView.automaticDimension
, 或返回 UITableView.automaticDimension
来自 tableView(_:heightForRowAt:)
关于ios - 为什么 UITableViewCell 缩小了 contentView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323467/