首页 > 解决方案 > 'UITableViewCell' 类型的值没有成员 'Job'

问题描述

我试图将数据加载到表格视图中,但出现错误 - 'UITableViewCell' 类型的值没有成员 'Job' & 'UITableViewCell' 类型的值没有成员'DateTime'。Job & DateTime 是我想在表格视图单元格中显示的变量。如何摆脱错误?

// MARK: - UITableViewDataSource
extension CalendarViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return displayName.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

        cell.Job.text = displayName[indexPath.row]
        cell.DateTime.text = displayDateTime[indexPath.row]

        return cell
    }
}

标签: iosswiftuitableview

解决方案


由于 Job 和 DateTime 不是 UITableviewCell 类的默认成员,您需要将 UITableViewCell 类型转换为您的自定义 tableviewcell 类,如图所示,

// MARK: - UITableViewDataSource
extension CalendarViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourTableCellClass

        cell.Job.text = displayName[indexPath.row]
        cell.DateTime.text = displayDateTime[indexPath.row]

        return cell
    }
}

推荐阅读