简体   繁体   English

使用 swift 创建自定义表格视图单元格

[英]Create a customized table view cell using swift

I am following this tutorial http://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views to create a customized table cell.我正在按照本教程http://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views创建自定义表格单元格。 But I am using swift instead of objective c.但我使用的是 swift 而不是目标 c。

Here is my table cell view in swift.这是我在 swift 中的表格单元格视图。

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)
    }
}

But it does not compile, I get error saying:但它不编译,我收到错误消息:

MyTableViewCell.swift:38:9: Property 'self.delegate' not initialized at super.init call MyTableViewCell.swift:38:9:属性“self.delegate”未在 super.init 调用时初始化

And if I get rid of "init(coder aDecoder: NSCoder)", I get error saying 'required' initializer 'init(coder:)' must be provided by subclass of UITableViewCell'.如果我去掉“init(coder aDecoder: NSCoder)”,我会收到错误提示“必需的”初始值设定项“init(coder:)”必须由 UITableViewCell 的子类提供。

I have read this thread Class does not implement its superclass's required members我读过这个线程Class does not implement its superclass's required members

about what to do in my required init(coder aDecoder: NSCoder).关于在我需要的 init(coder aDecoder: NSCoder) 中做什么。

But I can't get the code to compile.但是我无法编译代码。

I appreciate if someone can help me.如果有人可以帮助我,我将不胜感激。

Thank you.谢谢你。

It's saying you need to have delegate defined at the end of all init methods, because that class variable is not set as an optional. 就是说,您需要在所有init方法的末尾定义委托,因为该类变量未设置为可选变量。 One approach would be to make it optional (meaning it can be nil) 一种方法是使其成为可选(意味着它可以为nil)

var delegate: SwipeableCellDelegate?

Read about initialization and Optional Property Types here 在此处了解有关初始化和可选属性类型的信息

There are 3 ways to tackle your problem, of varying beauty and amounts of hassle.有 3 种方法可以解决您的问题,各有各的优点和麻烦。

  1. If you just need the code to compile, but will never use the required init from coder, you can just leave the default error in there.如果您只需要编译代码,但永远不会使用编码器所需的初始化,则可以将默认错误保留在那里。

  2. Also you can make the delegate field optional, but you will need to handle the theoretical possibility of a nil in your code from now on.您也可以将委托字段设置为可选,但从现在开始,您需要处理代码中 nil 的理论上的可能性。 Like this:像这样:

    var delegate: SwipeableCellDelegate? var 委托:SwipeableCellDelegate?

    guard let dlgt = delegate else {print("Delegate is not set for this cell"); guard let dlgt = delegate else {print("Delegate is not set for this cell"); return}返回}

  3. Or you can define a default value for a delegate somewhere public and set it in the required init.或者您可以在公共某处为委托定义默认值,并将其设置在所需的 init 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM