如何为自定义UITableViewCell类设置初始值设定项?

时间:2015-04-12 18:03:49

标签: ios uitableview swift initialization init

我已经四处寻找答案,但每个解决方案都会带来新的编译错误。

如何为从UITableViewCell继承的类设置初始值设定项?

这是我到目前为止所提供的任何帮助都将不胜感激:

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)


    }
   ...

}

修复错误:

enter image description here

编译错误:

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

1 个答案:

答案 0 :(得分:2)

当你拨打这一行时:

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

您需要提供实际的单元格样式和实际的重用标识符。此代码编写时,就好像这一行是函数定义,而不是函数调用。

将其更改为:

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

(但最好至少应该由调用者提供重用标识符,而不是静态)

相关问题