0

Xcode is sending me the error "fatal error: init(coder:) has not been implemented error" despite being implemented. I don't know where the problem is.

final class newTableViewController: UITableViewController {

    @IBOutlet var inputFormTableView: UITableView!

    let form: Form
    let note = Note(name: "")

    init(form: Form) {
        self.form = form
        super.init(style: .grouped)
    }

    convenience init(note: Note) {
        let form = Form(sections: [
            FormSection(items: [
                TextInputFormItem(text: note.name,
                                  placeholder: "Add title",
                                  didChange: { _ in print("hello")})
                ])
            ])
        self.init(form: form)
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()

        inputFormTableView.rowHeight = 44
        inputFormTableView.register(TextInputTableViewCell.self, forCellReuseIdentifier: ReuseIdentifiers.textInput.rawValue)
        let new = newTableViewController(note: note)
        print(new.note.name)

    }

    private enum ReuseIdentifiers: String {
        case textInput
    }

    // MARK: - Table view data source

    private func model(at indexPath: IndexPath) -> FormItem {
        return form.sections[indexPath.section].items[indexPath.item]
    }

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return form.sections[section].items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let object = model(at: indexPath)
        if let textRow = object as? TextInputFormItem {
            let cell = tableView.dequeueReusableCell(withIdentifier: ReuseIdentifiers.textInput.rawValue, for: indexPath) as! TextInputTableViewCell
            cell.configure(for: textRow)
            return cell
        }
        else {
            fatalError("Unknown model \(object).")
        }
    }

}

I am trying to make UITableView act like an input form. For doing this, I am following this tutoriel : https://augmentedcode.io/2018/11/04/text-input-in-uitableview/. Everything works in the sample project but not mine.

1
  • The error is right: init?(coder: NSCoder) is not implemented. The code to initialize form and the super call is missing. Commented May 22, 2020 at 17:00

1 Answer 1

2

You haven't implemented it, you are just throwing a fatalError, when running from stroy-board this init is getting executed, replace the following code:

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

With this:

required init?(coder: NSCoder) {
    form = Form()
    super.init(coder: coder)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.