首页 > 解决方案 > 表视图项目未显示

问题描述

我是 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
    }

}

关于发生了什么的任何建议?

标签: iosswiftuitableviewtableview

解决方案


条件 1:
如果您使用的是原型单元格,请删除该行

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

并在情节提要中给出单元格标识符

在此处输入图像描述

条件 2: 如果您使用的是 xib 文件,则注册单元格,如下所示:-

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

推荐阅读