0

I'm getting an error when trying to create a custom Class for my iOS Swift application:

Super.init called before returning from initializer

and

Will never be executed

Here is my code:

class CardTableViewCell: UITableViewCell {

    //Vars
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!
    @IBOutlet weak var thumbImage: UIImageView!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var costLabel: UILabel!

    init(name: String){
        self.nameLabel.text = name
    } //<-- Error here

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        super.init(coder:aDecoder) //<--- Error Here
    }

}

What am I doing wrong here? I've never had to add the init Coder method to a custom class before.

I've partially fixed the code, see below.

Updated Code: class CardTableViewCell: UITableViewCell {

    //Vars
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!
    @IBOutlet weak var thumbImage: UIImageView!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var costLabel: UILabel!

    init(name: String, subtitle: String) {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: name)
        self.nameLabel.text = name
        self.subtitleLabel.text = subtitle
    }

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

var lers = CardTableViewCell(name: "Happy", subtitle: "Gilmore") //<-- ERROR HERE
print(lers.subtitleLabel.text)

Error is returning:

Execution was interupted, reason EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I don't understand the how, what, or why for the init code. If someone can point me to some proper documentation on Swift 2, please show me. All of apples development documents are dreadful.

wUmpage
  • 165
  • 2
  • 17

3 Answers3

1

Change to:

init(name: String) {
    super.init(style: UITableViewCellStyle.Default, reuseIdentifier: name)
    self.nameLabel.text = name
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
JDG
  • 540
  • 1
  • 5
  • 16
  • This worked, but can you explain whats going on here? What is reuseIdentifier for? Would i have to call it for every variable on the class? For example how would this be called?: init(name: String, subtitle: String) { super.init(style: UITableViewCellStyle.Default, reuseIdentifier: name) self.nameLabel.text = name self.subtitleLabel.text = subtitle } – wUmpage Oct 22 '15 at 18:39
  • You call it as CardTableViewCell(name: yournameindentifier) to init. Or you can change the reuseIdentifier to a constant string according to your needs. Please accept this answer if it helped you. – JDG Oct 22 '15 at 18:44
  • So, reuseIdentifier can be a totally useless value? Its just needed to initialize the object? I'm not sure I understand. – wUmpage Oct 22 '15 at 21:18
  • If you want to reuse that cell, it should be a constant string. Otherwise, it really doesn't matter. – JDG Oct 22 '15 at 21:21
  • So how can I initialize multiple variables properly? Please see my updated code above. Thank you so far. – wUmpage Oct 22 '15 at 21:29
  • It seems you are fairly new to the concept of UITableViewCell. I would recommend reading https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html It is in Obj-C but you should get the general idea. Before you understand the concept, providing direct answers won't help you much. – JDG Oct 22 '15 at 21:40
  • All of these examples and documentation is either old or in Obj-C, does apple provide the same thing for Swift 2? – wUmpage Oct 22 '15 at 21:58
  • No. That's why I gave you this link – JDG Oct 22 '15 at 22:01
  • More rubbish Apple documentation. – wUmpage Oct 23 '15 at 14:15
0

Try changing this

init(name: String){
        self.nameLabel.text = name
    } //<-- Error here

To this :

init(name: String){
        self.nameLabel.text = name
        super.init(name: name)

    } //<-- Error here
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
  • Was given the error "Cannot invoke 'UITableViewCell.init' with an argument list of type '(name:String)' – wUmpage Oct 22 '15 at 18:41
0

It's not the direct answer to your question, but bear in mind that because your subclass is of UITableViewCell you're probably going to be getting cells indirectly via dequeueReusableCellWithIdentifier(_:) rather than constructing then directly.

So instead of than passing in a name property as a custom initializer, you might just want to assign this as part of the cell lifecycle (e.g. in tableView(_:cellForRowAtIndexPath:) or tableView(_:willDisplayCell:forRowAtIndexPath:))

MathewS
  • 2,267
  • 2
  • 20
  • 31