UITableViewCell无法展开以完全适合内容视图中的项目

问题描述 投票:0回答:1

我正在尝试布局包含UITableViewCellUIImageViewUILabel

我已经用]配置了表格视图>

    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableView.automaticDimension

我正在将我的视图添加到表视图内容视图。标签被切断,但是它是单行标签。

cutoffcell

import UIKit

final class FeedImageItem: BaseFeedItemCell {

  var onRetry: (() -> Void)?

  private(set) var itemImageView = configure(UIImageView(frame: .zero), using: {
    $0.translatesAutoresizingMaskIntoConstraints = false
  })

  private(set) var publishedAtLabel = configure(UILabel(frame: .zero), using: {
    $0.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)>

我正在尝试布局包含UIImageView和UILabel的UITableViewCell。我已经用tableView.estimatedRowHeight = 44 tableView.rowHeight = UITableView配置了表格视图。

ios swift uitableview autolayout uilabel
1个回答
0
投票

似乎您正在将标签添加并限制到contentView。但是,contentView行为由UIKit处理。每次执行此操作时,您都需要知道contentView到底发生了什么,以及它的用途。这是contentView的文档链接。从您的代码中,我可以收集到的是contentView约束需要设置为UITableViewCell。不要忘记将translatesAutoresizingMaskIntoConstraints设置为false,并且由于它已经是子视图,因此您无需再次添加它。在我看来,最好构建一个自定义的contentView并将所有方面约束到UITableViewCell。希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.