繁体   English   中英

Swift 编程表格视图不显示单元格

[英]Swift Programmatic TableView Not Showing Cells

我创建了一个自定义表格视图单元格,并尝试使用如下所示的测试字符串加载单元格。 表格视图是在代码中创建的,并显示在设备上,但未加载单元格。 所以我认为(但我可能错了)我的自定义单元格 class 或我的扩展代码中存在问题。

自定义 TableView 单元格

import UIKit

class TableViewCell: UITableViewCell {
    
    var titleView = UILabel()
    var descriptionView = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(titleView)
        addSubview(descriptionView)
        
        configureTitleView()
        configureDescriptionView()
        
        setTitleConstraints()
        setDescriptionConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(title: String, description: String){
        titleView.text = title
        descriptionView.text = description
    }
    
    func configureTitleView() {
        titleView.numberOfLines = 0
        titleView.adjustsFontSizeToFitWidth = true
    }
    
    func configureDescriptionView(){
        descriptionView.numberOfLines = 0
        descriptionView.adjustsFontSizeToFitWidth = true
    }
    
    
    func setTitleConstraints(){
        titleView.translatesAutoresizingMaskIntoConstraints = false
        titleView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        titleView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12).isActive = true
        titleView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        titleView.widthAnchor.constraint(equalToConstant: frame.width / 2 ).isActive = true
    }
    
    func setDescriptionConstraints(){
        descriptionView.translatesAutoresizingMaskIntoConstraints = false
        descriptionView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        descriptionView.leadingAnchor.constraint(equalTo: titleView.trailingAnchor, constant: 12).isActive = true
        descriptionView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        descriptionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12).isActive = true
    }

}

在 ViewDidLoad 中调用的方法- 这有效

 // MARK: - Base Table View Set UP
    private func TableViewSetUp(){

        tableView?.delegate = self
        tableView?.dataSource = self
        
        tableView = UITableView(frame: .zero, style: .plain)
        tableView?.backgroundColor = .brown // Testing Only
        tableView?.rowHeight = 100
        tableView?.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
        
        guard let myTableView = tableView else { return }
        view.addSubview(myTableView)
        
        // Constraints
        myTableView.translatesAutoresizingMaskIntoConstraints = false
        myTableView.topAnchor.constraint(equalTo: (collectionView?.bottomAnchor)!).isActive = true
        myTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        myTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        
    }

我如何尝试添加单元格信息

extension LogViewController: UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        
    }
}

extension LogViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        print("IN CELLFORROWAT")
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell 
        cell.set(title: "BOOO", description: "aaa")
        cell.backgroundColor = .blue
        return cell
    }
}

tableView?.delegate = self tableView?.dataSource = self

这些应该是在您创建 TableView 之后

所以而不是

private func TableViewSetUp(){
    tableView?.delegate = self
    tableView?.dataSource = self
    
    tableView = UITableView(frame: .zero, style: .plain)
    tableView?.backgroundColor = .brown // Testing Only
    tableView?.rowHeight = 100
    tableView?.register(TableViewCell.self, forCellReuseIdentifier: "Cell")

用这个

private func TableViewSetUp(){
    tableView = UITableView(frame: .zero, style: .plain)
    tableView?.delegate = self
    tableView?.dataSource = self
    tableView?.backgroundColor = .brown // Testing Only
    tableView?.rowHeight = 100
    tableView?.register(TableViewCell.self, forCellReuseIdentifier: "Cell")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM