我正在开发应用程序,需要在单元格中添加标签和图像,我使用此代码完成了此操作
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
}
}
但是当我运行模拟器时,什么也没发生,并且所有单元都是黑色的,没有任何大小变化(应该是)
更改单元格配置(类“ CustomGroupCell”不会更改结果。也许我错过了什么?我认为RecipesViewController中存在问题。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 245
}