簡體   English   中英

表視圖項目未顯示

[英]Table View items are not showing

我是 iOS 開發的新手。

我正在嘗試使用UITableView在我的應用程序中顯示項目列表,但項目沒有顯示:

我的視圖控制器:

class HistoryView: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    let cellReuseIdentifier = "cell"
    
    @IBOutlet weak var noDataLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(HistoryTableViewCell.self, forCellReuseIdentifier: "historyCell")
        
        tableView.delegate = self
        tableView.dataSource = self
        self.noDataLabel.isHidden=true
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell") as! HistoryTableViewCell

        cell.nickname?.text = "name"
        
        return cell
    }

    
}

我的表格視圖單元格:

class HistoryTableViewCell: UITableViewCell {
    
    @IBOutlet weak var nickname: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

對正在發生的事情有什么建議嗎?

條件 1:
如果您使用原型單元格,請刪除該行

tableView.register(HistoryTableViewCell.self, forCellReuseIdentifier:"historyCell")

並在故事板中給出單元格標識符

在此處輸入圖片說明

條件 2:如果您使用的是 xib 文件,則注冊如下單元格:-

tableView.register(UINib(nibName: "HistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "historyCell")

你需要像這樣注冊單元格

tb.register(UINib(nibName: "HistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "historyCell")

我看到@IBOutlet weak var nickname: UILabel!

class HistoryTableViewCell


對於表格單元格,

dequeueReusableCellregister ,應該成對調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM