ios - 使用 swift 创建自定义表格 View 单元格

标签 ios swift

我正在学习本教程 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views创建自定义表格单元格。但我使用的是 swift 而不是 objective c。

这是我在 swift 中的表格单元格 View 。

protocol SwipeableCellDelegate {
    func buttonOneActionForItemText(itemText: NSString)
    func buttonTwoActionForItemText(itemText: NSString)
}

class MyTableViewCell: UITableViewCell {
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var mytextLabel: UILabel!

    @IBOutlet weak var testLabel: UILabel!

    var delegate: SwipeableCellDelegate

    init(delegate: SwipeableCellDelegate) {
        self.delegate = delegate
        super.init()
    }

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

但它没有编译,我收到错误提示:

MyTableViewCell.swift:38:9: 属性 'self.delegate' 未在 super.init 调用时初始化

如果我去掉“init(coder aDecoder: NSCoder)”,我会得到错误提示 'required' initializer 'init(coder:)' 必须由 UITableViewCell' 的子类提供。

我读过这篇文章 Class does not implement its superclass's required members

关于在我需要的 init(coder aDecoder: NSCoder) 中做什么。

但我无法编译代码。

如果有人能帮助我,我将不胜感激。

谢谢。

最佳答案

这意味着您需要在所有初始化方法的末尾定义委托(delegate),因为该类变量未设置为可选。一种方法是将其设为可选(意味着它可以为零)

var delegate: SwipeableCellDelegate?

阅读有关初始化和可选属性类型的信息 here

关于ios - 使用 swift 创建自定义表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852875/

相关文章:

ios - 如何引用在 Storyboard 中创建的日期选择器并为其赋予默认值?

ios - Gigya Swift SDK:“无效的请求签名”

ios - 当我更新子值时在 firebase 中添加新子项 - ios

ios - 从 Fabric Crashlytics 迁移到 Firebase Crashlytics

ios - (非原子、强)属性的生命周期在 iOS 中如何运作?

ios - 如何在 iOS 的 UIDatePicker 中显示从现在起仅 7 天?

ios - 如何批量验证IOS内购收据?

swift - UITextView 分页不滚动

swift - 如何从 Swift 3 中的 TextView 中删除查找和共享

ios - MasterPass 集成 iOS