如何解决UITableView 自定义单元格高度不起作用
我正在学习如何以编程方式使用 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)
}
}
}
}
解决方法
您尚未实施 UITableViewDelegate
在您的 setupTable()
中添加行
tableView.delegate = self
然后更改您的扩展名
extension ViewController: UITableViewDataSource {
到extension ViewController: UITableViewDataSource,UITableViewDelegate {
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。