首页 > 解决方案 > 无法将单元格转换为自定义单元格

问题描述

我制作了一个自定义单元格,但它没有被调用或强制转换。

我尝试了不同的方法来调用标识符,注册单元格,但没有任何效果,我有一个示例项目,它正在工作,我有它完全相同。所以我不知道是什么原因造成的。

目前这些是我的功能。调试,我发现它从来没有进入过里面的if条件,cellForRowAt所以这就是问题所在,但无法弄清楚是什么原因造成的。

func registerCell() {
        let cellNib = UINib(nibName: FAQTableViewCell.reuseIdentifier, bundle: nil)
        tableView.register(cellNib, forCellReuseIdentifier: FAQTableViewCell.reuseIdentifier)
    }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: FAQTableViewCell.reuseIdentifier, for: indexPath)
        if let cell = cell as? FAQTableViewCell {
            cell.preguntaLabel.text = arrayTitulo[indexPath.row] as? String
            cell.respuestaLabel.text = arrayDesc[indexPath.row] as? String
        }

        return cell
    }

我检查了所有内容,单元格的 .xib、cell.swift 和 controller.swift,所有内容都匹配。我不知道还能做什么。

FAQTableViewCell

import AEAccordion

final class FAQTableViewCell: AccordionTableViewCell {

    static let reuseIdentifier = "FAQTableViewCell"

    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var detailView: UIView!
    @IBOutlet weak var preguntaLabel: UILabel!
    @IBOutlet weak var respuestaLabel: UILabel!

    // MARK: Override

    override func setExpanded(_ expanded: Bool, animated: Bool) {
        super.setExpanded(expanded, animated: animated)

        if animated {
            UIView.transition(with: detailView, duration: 0.3, animations: {
                self.detailView.isHidden = !expanded
            }, completion: nil)
        } else {
            detailView.isHidden = !expanded
        }
    }

    private func toggleCell() {
        detailView.isHidden = !expanded
    }
}

已修复:我刚刚删除了 .xib 并在 Main.storyboard 的 TableViewController 中创建了自定义单元格

标签: iosswiftuitableview

解决方案


我有同样的问题。在 Identity Inspector 中,Custom Class Inherit Module from Target 下方没有被选中,再次检查后,它被完美地转换了。


推荐阅读