使用自定义单元类时的Tableview错误

spo*_*oax 0 xcode uitableview ios swift

我试图以编程方式使用自定义单元类创建tableViewController,但是我收到此错误:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使用标识符cellId对单元格进行出列 - 必须为标识符注册一个nib或类或在故事板中连接原型单元'***First throw call stack:

我在我的应用程序中有几个UItableViews与自定义单元格,他们都工作正常,但由于某种原因,这一个不起作用.我花了最后几个小时试图解决这个问题,但看不出问题,有人可以帮帮我吗?

这是我的tableview类:

var tableView = UITableView()
    var twoDimensionalArray = [
        cameraInfoStruct(cameraInfo: ["Camera Body","Lens", "FPS", "Shutter Angle", "Colour Temperature", "Resolution Width", "Resolution Height", "Format", "Camera Rig"]),
        cameraInfoStruct(cameraInfo:["Focus Length", "F - Stop", "Camera Height", "Camera Tilt", "Camera Roll", "Filter"])
    ]

    let cellId = "cellId"
    let screenHeight = UIScreen.main.bounds.height
    let screenWidth = UIScreen.main.bounds.width
    let sectionHeaders = ["General", "Advanced"]

    override func viewDidLoad() {
        super.viewDidLoad()

        addTableView()
    }

    func addTableView() {
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView()
        self.view.addSubview(tableView)
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        if let sectionHeaderArray = sectionHeaders as? [String] {
            return sectionHeaderArray[section]
        }
        return "unknown"
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return twoDimensionalArray[section].cameraInfo.count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionHeaders.count
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = UIColor.white
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! cameraInfoCell
        let shotNumber = twoDimensionalArray[indexPath.section].cameraInfo[indexPath.row]

        cell.textLabel?.text = shotNumber

        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的自定义单元类:

class cameraInfoCell: UITableViewCell {

    override func layoutSubviews() {
        super.layoutSubviews()

        self.textLabel?.frame.origin.x = 20
        self.detailTextLabel?.frame.origin.x = 20
        self.detailTextLabel?.textColor = UIColor(red:0.73, green:0.73, blue:0.73, alpha:1.0)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

wot*_*tle 5

您使用两个不同的cellID:

在您的课程中,您定义了一个cellId变量,

let cellId = "cellId"
Run Code Online (Sandbox Code Playgroud)

您用于cellForRowAt:

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! cameraInfoCell
Run Code Online (Sandbox Code Playgroud)

addTableView,您使用硬编码字符串"MyCell":

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
Run Code Online (Sandbox Code Playgroud)

要解决此问题,请在两个位置使用cellId,因此请将addTableView方法中的代码更改为:

    tableView.register(cameraInfoCell.self, forCellReuseIdentifier: cellId)
Run Code Online (Sandbox Code Playgroud)