1

So Ive built a custom cell class for my UITableView. Xcode insists that this UITableViewCell has the following initializer (Xcode actually auto-fills this code for me):

class customCell: UITableViewCell {

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

Ive modified the contents of this initializer to contain the following:

        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)

If I remove the keyword required then Xcode kicks out an error and wants to correct me by putting the keyword back in.

So in my UITableView class I try to register this cell like so:

override func viewDidLoad(){
    super.viewDidLoad()
    let coder: NSCoder = NSCoder()
    let customCell: AnyClass = customCell(coder: coder)
    table?.registerClass(customCell, forCellReuseIdentifier:"cell")

The penultimate line (the one where I initialize the customCell with the NSCoder, as Xcode wants me to) produces the error:

Cannot invoke 'init' with an argument list of type '(coder: NSCoder)'

If I can I'd prefer to build my own initializer without an NSCoder.

If I try to use any other initializer then Xcode kicks out an error along the lines of:

Extra argument 'reuseIndenifier' in call

or

Missing argument in call

It will continue complaining until I am using the initializer with NSCoder. I am forced into using this initializer.

Please correct me if I am way off here, but to me it seems like I am caught in an catch 22 situation - Xcode complains if I build an initializer without a coder, and then when I try to use an initializer with a coder it complains that I am using a coder.

Despite this apparent contradiction I am fairly convinced all of this is a result of me being a dumbass somewhere.

Would anyone out there be kind enough to point me in the right direction?

12
  • did you read the UITableView programming guide? afaik this isn't how you use UITableView and UITableViewCell's, but I may be wrong. The initWithCoder initializer is called when using storyboards and that shouldn't happen because you typically initialize UITableViewCell objects in your UITableViewDataSource delegate method. So perhaps a bit more context would help here. Commented Nov 25, 2014 at 11:53
  • I am trying to create a custom UITableViewCell programmatically - please see this question: stackoverflow.com/questions/27103278/… Commented Nov 25, 2014 at 11:55
  • can you try using init(style style: UITableViewCellStyle, reuseIdentifier reuseIdentifier: String?), like I said, initWithCoder is called by interface builder Commented Nov 25, 2014 at 12:01
  • I tried this initially. Xcode complains that there is no required init(coder aDecoder:NSCoder) method and auto-completes it. Is this another Xcode bug? It has been suggested to me that I re-install Xcode. Commented Nov 25, 2014 at 12:09
  • making sure your IDE is working properly might be a start though I'm not sure that would solve your problem in particular. So leave the initWithCoder there, but just use initWithStyle Commented Nov 25, 2014 at 12:17

1 Answer 1

1

You need to register the class itself not an instance of it.

eg.

tableView?.registerClass(customCell.self, forCellReuseIdentifier:"cell")

The issue with requiring init(coder aDecoder:NSCoder) is a separate one and discussed here: 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`

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.