首页 > 解决方案 > 不显示我在单元格中使用锚点锚定的元素。怎么修?

问题描述

我正在开发一个应用程序,我需要向单元格添加一个标签和一个图像,我用这段代码做到了这一点

class CustomGroupCell: UITableViewCell {

var cellTitle = UILabel()
var imageViewContainerForShadow = UIView()
var imageViewForCell = UIImageView()

override func awakeFromNib() {
    super.awakeFromNib()
    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.heightAnchor.constraint(equalToConstant: 245).isActive = true
    contentView.addSubview(cellTitle)
    contentView.addSubview(imageViewContainerForShadow)
    imageViewContainerForShadow.addSubview(imageViewForCell)

    setupCellTitle()
    setupImageViewContainer()
    setupImageForCell()
}

private func setupCellTitle() {
    cellTitle.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 44).isActive = true
    cellTitle.topAnchor.constraint(equalTo: contentView.topAnchor, constant: -4).isActive = true
    cellTitle.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -163).isActive = true
}

private func setupImageViewContainer() {
    imageViewContainerForShadow.topAnchor.constraint(equalTo: cellTitle.bottomAnchor, constant: -10).isActive = true
    imageViewContainerForShadow.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 20).isActive = true
    imageViewContainerForShadow.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 20).isActive = true
    imageViewContainerForShadow.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -20).isActive = true
    imageViewContainerForShadow.widthAnchor.constraint(equalTo: imageViewContainerForShadow.heightAnchor, multiplier: 373.0/199.0).isActive = true

}

private func setupImageForCell() {
    imageViewForCell.trailingAnchor.constraint(equalTo: imageViewContainerForShadow.trailingAnchor).isActive = true
    imageViewForCell.leadingAnchor.constraint(equalTo: imageViewContainerForShadow.leadingAnchor).isActive = true
    imageViewForCell.topAnchor.constraint(equalTo: imageViewContainerForShadow.topAnchor).isActive = true
    imageViewForCell.bottomAnchor.constraint(equalTo: imageViewContainerForShadow.bottomAnchor).isActive = true
}

class RecipesViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(CustomGroupCell.self, forCellReuseIdentifier: "customGroupCell")

    view.backgroundColor = UIColor(named: "AppBackgroundColor")
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customGroupCell", for: indexPath) as! CustomGroupCell

    cell.cellTitle.text = "TESTING"
    cell.imageViewForCell.backgroundColor = .blue

    return cell
}

}

但是当我运行模拟器时,什么都没有发生,所有的单元格都是黑色的,没有任何大小变化(它们应该是)

问题

更改单元格配置(类“CustomGroupCell”不会改变结果。也许我错过了什么?我认为RecipesViewController 有问题。

标签: iosswift

解决方案


问题解决layoutSubviews()awakeFromNib()


推荐阅读