首页 > 解决方案 > 为什么我不能将此单元格快速转换为从单元格继承的类?

问题描述

我正在TableView使用我在其他几个应用程序中完成的 swift创建一个UITableViewCOntroller,但是这次当我尝试将我的单元格转换为自定义单元格类时,我收到无法转换的错误。

这是我的代码:

class CollapseNoteViewController: UITableViewController {

    let titleArray = ["Troy and Abed", "Shirly and Annie", "Pierce and Ben"]
    let noteArray = ["The best", "Pretty cool", "The worst"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // This is where I get the error
        let cell = tableView.dequeueReusableCell(withIdentifier: K.reuseIdentifier, for: indexPath) as! NoteTableViewCell 
        
        cell.titleLabel?.text = titleArray[indexPath.row]
        cell.textLabel?.text = noteArray[indexPath.row]
        
        return cell
    }
    
}

class NoteTableViewCell: UITableViewCell {

    @IBOutlet weak var cardView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var mainTextLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
}

完整的错误文本:

Could not cast value of type 'UITableViewCell' (0x1eccdbb70) to 'CollapseANote.NoteTableViewCell' (0x104d3ced8).
Could not cast value of type 'UITableViewCell' (0x1eccdbb70) to 'CollapseANote.NoteTableViewCell' (0x104d3ced8).
2021-02-18 10:36:47.613801-0500 CollapseANote[639:74162] Could not cast value of type 'UITableViewCell' (0x1eccdbb70) to 'CollapseANote.NoteTableViewCell' (0x104d3ced8).

我在哪里设置原型单元的类:

在此处输入图像描述

我在哪里设置课程TableViewController

在此处输入图像描述

标签: iosswiftuitableview

解决方案


您嵌套到表格单元格并将类名设置为内部的,这就是为什么 ist 在这里不可见

在此处输入图像描述

把里面的拖出来就好了

在此处输入图像描述

然后删除 NoteItemCell

在此处输入图像描述

如果它代表另一个不同的单元格,则由您决定


推荐阅读