ios - 不显示我在单元格中使用锚点锚定的元素。怎么修?
问题描述
我正在开发一个应用程序,我需要向单元格添加一个标签和一个图像,我用这段代码做到了这一点
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 有问题。
解决方案
问题解决layoutSubviews()
了awakeFromNib()
推荐阅读
- cadence-workflow - Uber Cadence 制作设置
- javascript - 在Vue.js中加载后如何为每个图像顺序设置动画?
- java - 在 Android 上单击 ImageButton 时播放随机声音
- clojure - 跨多个命名空间使用 clojure 协议
- angularjs - angularJs http Post 问题:插入数据
- python - 将多个 Pandas 数据框样式呈现到一个 HTML 电子邮件正文
- deep-learning - 获取 inceptionV3 的输入尺寸不匹配的运行时错误
- javascript - 从数据库 discord.js 加入后更改用户昵称
- c++ - 使用 C++ 错误构建 Opencv:未定义对 `cv::Mat::deallocate()' 的引用
- api - WSO2 组件的启动和关闭顺序