【发布时间】:2021-09-17 10:44:31
【问题描述】:
我正在学习如何以编程方式使用 UIKit,并创建了一个自定义 UITableViewCell,但我的行高没有注册。
我不断收到此错误
[警告] 仅警告一次:检测到约束不明确的情况 建议表格视图单元格的内容视图高度为零。 我们正在考虑意外折叠并改用标准高度。
即使在谷歌搜索错误之后,我似乎也无法理解/找到我的代码的问题所在。
视图控制器:
import UIKit
class ViewController: UIViewController {
let contacts = ContactAPI.getContacts()
let tableView = UITableView()
var safeArea: UILayoutGuide!
//let contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
safeArea = view.layoutMarginsGuide
setUpTable()
setUpNavigation()
}
func setUpTable(){
view.addSubview(tableView)
//populate with data
tableView.dataSource = self
tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
//turn off autoresizing
tableView.translatesAutoresizingMaskIntoConstraints = false
//Layout Configs
tableView.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
func setUpNavigation(){
navigationItem.title = "Contacts"
self.navigationController?.navigationBar.barTintColor = .gray
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}
}
extension ViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
//cell.textLabel?.text = contacts[indexPath.row].name
cell.contact = contacts[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0
}
}
自定义单元:
class TableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(profileImage)
subView.addSubview(nameLabel)
subView.addSubview(jobTitleLabel)
self.contentView.addSubview(subView)
self.contentView.addSubview(flagImage)
profileImage.centerYAnchor.constraint(equalTo:self.contentView.centerYAnchor).isActive = true
profileImage.leadingAnchor.constraint(equalTo:self.contentView.leadingAnchor, constant:10).isActive = true
profileImage.widthAnchor.constraint(equalToConstant:70).isActive = true
profileImage.heightAnchor.constraint(equalToConstant:70).isActive = true
subView.centerYAnchor.constraint(equalTo:self.contentView.centerYAnchor).isActive = true
subView.leadingAnchor.constraint(equalTo:self.profileImage.trailingAnchor, constant:10).isActive = true
subView.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor, constant:-10).isActive = true
subView.heightAnchor.constraint(equalToConstant:40).isActive = true
nameLabel.topAnchor.constraint(equalTo:self.subView.topAnchor).isActive = true
nameLabel.leadingAnchor.constraint(equalTo:self.subView.leadingAnchor).isActive = true
nameLabel.trailingAnchor.constraint(equalTo:self.subView.trailingAnchor).isActive = true
jobTitleLabel.topAnchor.constraint(equalTo:self.nameLabel.bottomAnchor).isActive = true
jobTitleLabel.leadingAnchor.constraint(equalTo:self.subView.leadingAnchor).isActive = true
jobTitleLabel.topAnchor.constraint(equalTo:self.nameLabel.bottomAnchor).isActive = true
jobTitleLabel.leadingAnchor.constraint(equalTo:self.subView.leadingAnchor).isActive = true
flagImage.widthAnchor.constraint(equalToConstant:26).isActive = true
flagImage.heightAnchor.constraint(equalToConstant:26).isActive = true
flagImage.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor, constant:-20).isActive = true
flagImage.centerYAnchor.constraint(equalTo:self.contentView.centerYAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
//Profile Image
let profileImage: UIImageView = {
let img = UIImageView()
img.contentMode = .scaleAspectFill
img.translatesAutoresizingMaskIntoConstraints = false
img.layer.cornerRadius = 35
img.clipsToBounds = true
return img
}()
//Name
let nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 20)
label.textColor = .gray
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
//Job Title
let jobTitleLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 14)
label.textColor = .white
label.backgroundColor = .gray
label.layer.cornerRadius = 5
label.clipsToBounds = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
//Container for Name and Job Title
let subView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.clipsToBounds = true
return view
}()
//Country Flag
let flagImage: UIImageView = {
let img = UIImageView()
img.contentMode = .scaleAspectFill
img.translatesAutoresizingMaskIntoConstraints = false
img.layer.cornerRadius = 13
img.clipsToBounds = true
return img
}()
var contact:Contact? {
didSet{
guard let contactItem = contact else {return}
if let name = contactItem.name{
profileImage.image = UIImage(named: name)
nameLabel.text = name
}
if let jobTitle = contactItem.jobTitle{
jobTitleLabel.text = "\(jobTitle)"
}
if let country = contactItem.country{
flagImage.image = UIImage(named: country)
}
}
}
}
【问题讨论】:
-
这个
jobTitleLabel.topAnchor.constraint(equalTo:self.nameLabel.bottomAnchor).isActive = true重复了两次。 -
谢谢,我删除了重复的部分,但没有任何改变。
-
从未说过它会修复错误。我刚注意到错字。但是,只有当我的代码中没有
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat时,我才会收到该错误。从您发布的代码来看,您似乎没有实现UITableViewDelegate。如果你实现它,那么它应该可以工作。 -
啊,我明白了!感谢您的帮助,这已解决。
标签: ios swift uitableview