TableView单元格的黑色,我无法配置单元格。如何解决?

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

我正在开发应用程序,需要在单元格中添加标签和图像,我使用此代码完成了此操作

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
}

also

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
}

}

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

problem

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

ios swift
1个回答
0
投票
要设置单元格的高度,请不要设置contentView的高度,而应使用以下tableView函数:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 245 }

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