【问题标题】:How do I set an initializer for a custom UITableViewCell class?如何为自定义 UITableViewCell 类设置初始化程序?
【发布时间】:2015-06-18 00:26:23
【问题描述】:

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

如何为继承自 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)


    }
   ...

}

修复错误:

编译器错误:

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

【问题讨论】:

    标签: ios uitableview swift initialization init


    【解决方案1】:

    当您拨打此行时:

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

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

    将其更改为:

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

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

    【讨论】:

    • 感谢您的响应,这是我使用您的签名时编译器返回的错误:“UITableViewCellStyle.Type”没有名为“UITableViewCellStyleDefault”的成员
    • 好的,刚刚改成:super.init(style: .Default, reuseIdentifier: "SettingCell")
    • 对不起,那是 obj-c 版本的枚举
    猜你喜欢
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2012-06-26
    • 2017-11-16
    相关资源
    最近更新 更多