【发布时间】:2017-02-22 22:43:12
【问题描述】:
我知道调整 UILabel 高度和 UITableViewCell 可能是一个非常标准的问题,但我找到了很多基于情节提要和检查器的答案,但不仅仅是使用 Swift 3。我创建了一个表覆盖屏幕。单元格的高度确定如下:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
我的 tableViewCell 中有几个对象,一个 UIImageView (myImage) 和一个 UILabel (myText),在自定义类中设置。定位和调整大小在 cellForRowAt 中进行。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.myImage.image = UIImage(named: "MyImage")
cell.myImage.layer.frame = CGRect(origin: CGPoint(x: 0, y: 10), size: (cell.myImage?.image?.size)!)
cell.myText.text = myArray[indexPath.row]
cell.myText.frame = CGRect(x: UIScreen.main.bounds.size.width * 0.25, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: 90)
cell.myText.numberOfLines = 0
return cell
}
结果是一堆堆叠的单元格,彼此重叠。我应该怎么做才能将 UILabel 框架的高度调整为来自 myArray 的文本量并调整单元格高度,使其至少是 myImage 或 myText 的高度?
【问题讨论】:
-
你用过自动布局吗?
-
我正在使用自动布局,感谢您提供前面问题的链接。我错过了在另一个问题中以编程方式为多个对象设置约束的示例。约束是从概念上描述的,例如参考 updateConstraints(),或使用界面生成器显示。关于如何将 UILabel、UIImageViews 等对象与单元格的 contentView 以及如何/何时调用 updateConstraints 相关联,您是否有更多解释?
-
如果我的评论或提供的链接对您有帮助,请点赞我的评论。谢谢
标签: uitableview swift3 uilabel