ios - 如何为自定义 UITableViewCell 类设置初始化程序?

标签 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'

最佳答案

当你调用这条线时:

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

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

将其更改为:

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

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

关于ios - 如何为自定义 UITableViewCell 类设置初始化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592701/

相关文章:

javascript - 在 IOS 上固定位置和倾斜

uitableview - 没有行动画的 ReloadRowsAtIndexPaths

ios - 如何使用 RxDataSources 避免重复点击 Tableview 上的按钮

ios - Xcode 找不到 Alamofire,错误 : No such module 'Alamofire'

ios - @3x 图像在 XIB 中的 iPhone 6 或 5S 或 5 上错误地使用了 "Use as Launch Image"选项集

ios - ReactNative iOS sendEventWithName 原因 "RCTCallableJSModules is not set"

ios - 使用与 App ID 相同的服务 ID 标识符

ios - swift 3 : TableView refresh doesn't work with row action

swift - 将 swift 3 项目转换为使用 CoreData

ios - 添加 JSON 数据时 UITableView 不工作