ios - UITableViewCell contentView insets 和自动布局

标签 ios swift uitableview

我的问题是,我想将单元格的 contentView 每个顶部、底部、左侧、右侧插入 10。我想使用这个空间作为分隔符并禁用默认的 TableView 分隔符。没有插图看起来像这样:

without separation

现在有两个问题:

  1. 问题:高度限制不明确(在 View 调试器中)
  2. 没有分隔符

1.

ambiguous height constraints

  1. 我可以通过覆盖我的表格 View 单元格的 layoutSubviews 来更改它:

    override func layoutSubviews() {
       super.layoutSubviews()
    
       let contentViewFrame = self.contentView.frame
       let insetContentViewFrame = contentViewFrame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
       self.contentView.frame = insetContentViewFrame
       self.selectedBackgroundView?.frame = insetContentViewFrame
    }
    

但这会导致问题 3:

issue no 3

现在描述标签有问题:

这是我的表格 View 单元格(它使用自动布局,因此标签定义了单元格高度):

class NewsTableViewCell : UITableViewCell {

public private(set) var titleLabel: UILabel!
public private(set) var descriptionLabel: UILabel!

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

    self.tintColor = UIColor.white

    self.contentView.backgroundColor = UIColor.barTintColor
    self.contentView.layer.masksToBounds = false
    self.contentView.layer.cornerRadius = 10.0

    self.backgroundColor = UIColor.clear

    let layoutGuide = self.contentView.layoutMarginsGuide

    self.titleLabel = UILabel(frame: .zero)
    self.titleLabel.numberOfLines = 0
    self.titleLabel.font = UIFont.preferredFont(forTextStyle: .headline)
    self.titleLabel.textColor = UIColor.white
    self.titleLabel.adjustsFontSizeToFitWidth = false
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
    self.titleLabel.textAlignment = .left
    self.contentView.addSubview(self.titleLabel)

    self.titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)

    self.titleLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    self.titleLabel.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    self.titleLabel.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true

    self.descriptionLabel = UILabel(frame: .zero)
    self.descriptionLabel.numberOfLines = 0
    self.descriptionLabel.font = UIFont.preferredFont(forTextStyle: .subheadline)
    self.descriptionLabel.textColor = UIColor.lightGray
    self.descriptionLabel.adjustsFontSizeToFitWidth = false
    self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
    self.descriptionLabel.textAlignment = .left
    self.contentView.addSubview(self.descriptionLabel)

    self.descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)

    self.descriptionLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    self.descriptionLabel.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    self.descriptionLabel.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: 5.0).isActive = true
    self.descriptionLabel.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

    let selectedView: UIView = UIView(frame: .zero)
    selectedView.backgroundColor = UIColor.gray
    selectedView.layer.cornerRadius = 10.0
    selectedView.layer.masksToBounds = false
    self.selectedBackgroundView = selectedView
}

这是我的 TableView Controller 代码:

self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 200.0
self.tableView.register(NewsTableViewCell.self, forCellReuseIdentifier: "NewsCell")
self.tableView.separatorStyle = .none

有什么办法既可以插入我的 contentView 又可以动态调整大小标签(定义单元格高度)? 谢谢!

最佳答案

1- 添加常量参数

 self.titleLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor,constant:10).isActive = true

前导、尾随、顶部和底部

2-

description Label底部约束的优先级降低到999

关于ios - UITableViewCell contentView insets 和自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53816550/

相关文章:

ios - 存折 "application/vnd.apple.pkpass"mime 类型无法识别

ios - 将异步 block 回调转换为同步返回值

iphone - 使用 uitextfields 创建 uitableview 表单

ios - 设置tableview的动态高度

ios - UITableViewCells 点击时不会改变颜色

ios - iPhone 6比较两个UIColors

ios - ffmpeg 转换后的 x264 视频显示色彩饱和度急剧下降

ios - NSMutableArray 和 NSArray 在 swift 中

ios - collectionview 只填充 iPhone 4-5 上的一个单元格

ios - 递归查找特定类型iOS的 View