`tableView.dequeueReusableCell(withIdentifier:for:)` 从父类调用初始化器

Mil*_*sáľ 4 uitableview ios swift

我有一个tableView使用继承的单元格。有一个共同的祖先,然后有几种具体的实现。公共类不会重写init(style:reuseIdentifier:),而是定义自己的自定义初始值设定项。然而,所有子类都定义了这个初始值设定项,它们在其中从超类中调用自定义的初始值设定项。现在,tableView我注册子类以供重用,并且将子类出队以供重用。然而,当我尝试将任何子类出队时,出现以下错误:

fatal error: use of unimplemented initializer 'init(style:reuseIdentifier:)' for class 'milan.BotChatBaseCell'
Run Code Online (Sandbox Code Playgroud)

milan.BotChatBaseCell超类在哪里。现在milan.BotChatBaseCell永远不会注册到tableView。知道为什么会发生这种情况吗?

代码:

import UIKit

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(ExampleBotChatCell.self, forCellReuseIdentifier: "ExampleBotChatCell")
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleBotChatCell", for: indexPath) as! ExampleBotChatCell
        return cell
    }
}

class BotChatBaseCell: UITableViewCell {
    init(style: UITableViewCellStyle, reuseIdentifier: String?, customString: String) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        print(">>>> nice \(customString)")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ExampleBotChatCell: BotChatBaseCell {

    init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier, customString: "Example")
    }

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

我刚刚测试过它,它可以在 Swift 3.2 上运行,但不能在 Swift 4 上运行。

mat*_*att 9

公共类不会重写 init(style:reuseIdentifier:) ,而是定义自己的自定义初始值设定项。

那就是问题所在。通过创建新的指定初始值设定项,您就放弃了继承,因此BotChatBaseCell不再继承指定初始值设定项init(style:reuseIdentifier:)。它必须实施这一点。

一种可能的解决方案是使新的初始化程序成为方便的初始化程序,从而允许继承继续工作。另一种可能的解决方案是添加 的覆盖init(style:reuseIdentifier:),即使它是微不足道的(即它只是调用super)。