簡體   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