Dan Beaulieu
Dan Beaulieu

Reputation: 19964

How do I set an initializer for a custom UITableViewCell class?

I've searched around for an answer to this, but each solution just brings new compiler errors.

How do I set up the initializers for a class that inherits from UITableViewCell?

This is what I have so far, any help would be greatly appreciated:

SettingCell.swift

class SettingCell: UITableViewCell {

    @IBOutlet weak var settingsLabel: UILabel!
    @IBOutlet weak var settingsSwitch: UISwitch?
    @IBOutlet weak var timeSetting: UILabel?

    var cellDelegate: SettingCellDelegate?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    init(settingLabel: UILabel!, settingSwitch: UISwitch?, timeSetting: UILabel?) {

        super.init(style: UITableViewCellStyle, reuseIdentifier: String?)
        self.settingsLabel = settingLabel
        self.settingsSwitch = settingSwitch
        self.timeSetting = timeSetting

    }

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

    @IBAction func handledSwitchChange(sender: UISwitch) {

        self.cellDelegate?.didChangeSwitchState(sender: self, isOn:settingsSwitch!.on)


    }
   ...

}

Fix-It Error:

enter image description here

Compiler Error:

'UITableViewCellStyle.Type' is not convertible to 'UITableViewCellStyle'

Upvotes: 1

Views: 2246

Answers (1)

Wain
Wain

Reputation: 119041

When you call this line:

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

you need to supply an actual cell style and an actual reuse identifier. You code at the moment is written as if this line is a function definition, not a function invocation.

Change it to something like:

super.init(style: .Default, reuseIdentifier: "SettingCell")

(though preferably at least the reuse identifier should be provided by the caller, not statically)

Upvotes: 2

Related Questions