Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`

This code has reportedly worked here and here, but I can't seem to make it work.

The IBOutlets are hooked up to their objects in the storyboard. The prototypeCell is named so I can use it with dequeueReusableCellWithIdentifier and it's custom class attribute is set to commentCell.

First Error (which I can solve, but neither of the links above needed it, which makes me think I'm doing something wrong. Am I right?):

Overriding method with selector 'initWithStyle:reuseIdentifier:' has incompatible type '(UITableViewCellStyle, String) -> commentCell' 

Second Error (the interesting error):

'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'` 

Cell Class Code:

class commentCell: UITableViewCell {     @IBOutlet weak var authorLabel: UILabel!     @IBOutlet weak var commentLabel: UITextView!      init(style: UITableViewCellStyle, reuseIdentifier: String) {         super.init(style: style, reuseIdentifier: reuseIdentifier)     }      override func awakeFromNib() {         super.awakeFromNib()     }      override func setSelected(selected: Bool, animated: Bool) {         super.setSelected(selected, animated: animated)     } } 

Initialization code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     println(comments[indexPath.row])      var cell = self.tableView.dequeueReusableCellWithIdentifier("prototypeCell") as commentCell      cell.commentLabel.text = comments[indexPath.row]["comment"] as NSString     cell.authorLabel.text = comments[indexPath.row]["fromid"] as NSString     return cell } 
like image 265
User Avatar asked Sep 28 '14 03:09

User


People also ask

What is required init coder Adecoder NSCoder?

If you subclass an object that implements NSCoding then you need to implement this initialiser, since it is required of classes that implement NSCoding . You must at least call the superclass init method. If the NSCoder contains encoded properties for your class then you can use this method to recover those.

What is required init in Swift?

Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.

What is init coder?

init(coder:)Returns an object initialized from data in a given unarchiver.

What are Initializers in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer.


1 Answers

The correct signature for the first initializer is this:

init(style style: UITableViewCellStyle, reuseIdentifier reuseIdentifier: String?) 

Notice that reuseIdentifier is an Optional, as indicated by the ?.

If you override any of a class's designated initializers, you don't inherit any other designated initializers. But UIView adopts the NSCoding protocol, which requires an init(coder:) initializer. So you must implement that one too:

init(coder decoder: NSCoder) {     super.init(coder: decoder) } 

Note, however, that you're not actually doing anything in either initializer except calling super, so you don't need to implement either initializer! If you don't override any designated initializers, you inherit all of your superclass's designated initializers.

So my advice is that you just remove your init(style:reuseIdentifier:) initializer entirely unless you're going to add some initialization to it.

And if you're planning to add some initialization to it, be advised that prototype cells in a storyboard are not initialized by init(style:reuseIdentifier:). They are initialized by init(coder:).

like image 53
rob mayoff Avatar answered Oct 01 '22 12:10

rob mayoff