Search code examples
iosswiftuitableviewsigabrt

SIGABRT error in dequeueReusableCell(withIdentifier:), when using custom UITableViewCell


I'm building an app with multiple scenes and a table view with custom cells in each. I got the home screen table view to work fine and then I segue to the new scene from the custom cells. When it segues, my second view controller crashes.

Here is my code for the view controller

import UIKit

class QuestionViewController: UIViewController {

    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var qTableView: UITableView!


    var answers : [QuestionOption] = []


    override func viewDidLoad() {
        super.viewDidLoad()
        answers = [QuestionOption(text: "test"), QuestionOption(text: "test"), QuestionOption(text: "test"), QuestionOption(text: "test")]
        qTableView.delegate = self
        qTableView.dataSource = self
        submitButton.setTitle("Submit", for: .normal)
        questionLabel.text = "test question"
    }


}

extension QuestionViewController: UITableViewDataSource, UITableViewDelegate{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return answers.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let a = answers[indexPath.row]
        let cell = qTableView.dequeueReusableCell(withIdentifier: "QuestionOptionCell") as! QuestionOptionCell

        cell.setOption(option: a)

        return cell
    }

}

Here's my code for the cell

import UIKit

class QuestionOptionCell: UITableViewCell {

    @IBOutlet weak var cellTitle: UILabel!

    func setOption(option: QuestionOption){
        cellTitle.text = option.text
    }

}

Here's my code for the QuestionOption class

import Foundation
import UIKit

class QuestionOption{
    var text: String

    init(text: String){
        self.text = text
    }
}

Crash log

2019-02-20 14:33:28.394695-0800 iQuiz[8935:822409] *** NSForwarding: warning: object 0x7fd608407c40 of class 'iQuiz.QuestionOption' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[iQuiz.QuestionOption initWithCoder:]
2019-02-20 14:33:28.395281-0800 iQuiz[8935:822409] Unrecognized selector -[iQuiz.QuestionOption initWithCoder:]

Here's my storyboard if that helps at all enter image description here

I've made sure my identifier matches and I don't have any extraneous or unconnected outlets, those are the only solution to this problem I can find online.


Solution

  • The crash log says that QuestionOption must be a subclass of NSObject and adopt NSCoding which is overkill in this case. Actually a struct would be sufficient.

    You can avoid it by deleting the method in QuestionOptionCell

    func setOption(option: QuestionOption){
        cellTitle.text = option.text
    }
    

    and set the value in cellForRowAt directly by replacing

    cell.setOption(option: a)
    

    with

    cell.cellTitle.text = a.text