UITableViewCell 未扩展以完全适合内容视图中的项目

UITableViewCell not expanding to fit items fully in content view

我正在尝试布局包含 UIImageViewUILabelUITableViewCell

我已经用

配置了我的 table 视图
    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableView.automaticDimension

我正在将我的观点添加到 table 视图内容视图中。标签被截断了,但它是单行标签。

import UIKit

final class FeedImageItem: BaseFeedItemCell {

  var onRetry: (() -> Void)?

  private(set) var itemImageView = configure(UIImageView(frame: .zero), using: {
    [=11=].translatesAutoresizingMaskIntoConstraints = false
  })

  private(set) var publishedAtLabel = configure(UILabel(frame: .zero), using: {
    [=11=].translatesAutoresizingMaskIntoConstraints = false
  })

  private(set) var itemTitleLabel = UILabel(frame: .zero)

  private(set) var retryButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("reload", for: .normal)
    return button
  }()

  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    configureBindings()
    configureUI()
  }

  required init?(coder: NSCoder) {
    return nil
  }
}

private extension FeedImageItem {
  @objc func retryButtonTapped() {
    onRetry?()
  }

  func configureBindings() {
    retryButton.addTarget(self, action: #selector(retryButtonTapped), for: .touchUpInside)
  }

  func configureUI() {

    [itemImageView, publishedAtLabel].forEach(contentView.addSubview(_:))

    NSLayoutConstraint.activate([
      itemImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
      itemImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
      itemImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
      itemImageView.heightAnchor.constraint(equalToConstant: 184),

      publishedAtLabel.topAnchor.constraint(equalTo: itemImageView.bottomAnchor, constant: 8),
      publishedAtLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
      publishedAtLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
      publishedAtLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
    ])

  }
}

如何将项目添加到我的内容视图并使单元格根据我添加的自动布局锚展开?

如果我直接将我的项目添加到视图中,我不会遇到这个问题,但我的印象是我应该添加到内容视图中。

我的内容视图如下所示:

open class BaseFeedItemCell: UITableViewCell {

  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    configureUI()
  }

  required public init?(coder: NSCoder) {
    return nil
  }

  open override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: .init(top: 8, left: 12, bottom: 8, right: 12))

    contentView.backgroundColor = .white
    contentView.layer.cornerRadius = 12
    contentView.layer.shadowOffset = .init(width: 0, height: 4)
    contentView.layer.shadowColor = UIColor(red: 0.18, green: 0.18, blue: 0.18, alpha: 0.16).cgColor
    contentView.layer.shadowRadius = 4
    contentView.layer.shadowOpacity = 1

    let shadowFrame = contentView.layer.bounds
    let shadowPath = UIBezierPath(rect: shadowFrame).cgPath
    contentView.layer.shadowPath = shadowPath

    contentView.clipsToBounds = true
  }
}

private extension BaseFeedItemCell {
  func configureUI() {
    backgroundColor = .clear
  }
}

通过在我的标签上设置高度 - publishedAtLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 44)

我也遇到自动布局问题。

(
    "<NSLayoutConstraint:0x600003671220 V:|-(0)-[UIImageView:0x7fee33523000]   (active, names: '|':UITableViewCellContentView:0x7fee335bd2b0 )>",
    "<NSLayoutConstraint:0x600003671310 UIImageView:0x7fee33523000.height == 184   (active)>",
    "<NSLayoutConstraint:0x600003671360 V:[UIImageView:0x7fee33523000]-(8)-[UILabel:0x7fee335bc100'Published 16 May 2020']   (active)>",
    "<NSLayoutConstraint:0x600003671400 UILabel:0x7fee335bc100'Published 16 May 2020'.bottom == UITableViewCellContentView:0x7fee335bd2b0.bottom - 8   (active)>",
    "<NSLayoutConstraint:0x6000036714a0 UILabel:0x7fee335bc100'Published 16 May 2020'.height >= 44   (active)>",
    "<NSLayoutConstraint:0x60000366df90 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fee335bd2b0.height == 228   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000036714a0 UILabel:0x7fee335bc100'Published 16 May 2020'.height >= 44   (active)>

似乎您正在将标签添加并限制到 contentView。但是,contentView 行为由 UIKit 处理。每次执行此操作时,您都需要了解 contentView 的具体情况及其用途。这是 link 到 contentView 的文档。从您的代码中,我只能收集到 contentView 约束需要设置为 UITableViewCell。不要忘记将 translatesAutoresizingMaskIntoConstraints 设置为 false 并且因为它已经是一个子视图,所以您不必再次添加它。在我看来,最好构建一个自定义 contentView 并将所有边都约束到 UITableViewCell。希望对你有帮助。