繁体   English   中英

'required'initulator'init(coder :)'必须由'UITableViewCell'的子类提供

[英]'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`

据报道,此代码在这里这里都有用,但我似乎无法使其工作。

IBOutlets连接到故事板中的对象。 prototypeCell是命名的,因此我可以将它与dequeueReusableCellWithIdentifier一起使用,并将其自定义类属性设置为commentCell

第一个错误(我可以解决,但上面的链接都不需要它,这让我觉得我做错了。我是对的吗?):

Overriding method with selector 'initWithStyle:reuseIdentifier:' has incompatible type '(UITableViewCellStyle, String) -> commentCell'

第二个错误(有趣的错误):

'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`

细胞类代码:

class commentCell: UITableViewCell {
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var commentLabel: UITextView!

    init(style: UITableViewCellStyle, reuseIdentifier: String) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

初始化代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    println(comments[indexPath.row])

    var cell = self.tableView.dequeueReusableCellWithIdentifier("prototypeCell") as commentCell

    cell.commentLabel.text = comments[indexPath.row]["comment"] as NSString
    cell.authorLabel.text = comments[indexPath.row]["fromid"] as NSString
    return cell
}

第一个初始化程序的正确签名是:

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

请注意, reuseIdentifier是一个Optional ,如?

如果覆盖任何类的指定初始值设定项,则不会继承任何其他指定的初始值设定项。 但是UIView采用了NSCoding协议,它需要一个init(coder:)初始化器。 所以你也必须实现那个:

init(coder decoder: NSCoder) {
    super.init(coder: decoder)
}

但是,请注意,除了调用super之外,您实际上并没有在任何初始化程序中执行任何操作,因此您不需要实现任何初始化程序! 如果不覆盖任何指定的初始值设定项,则继承所有超类的指定初始值设定项。

所以我的建议是你只需要完全删除你的init(style:reuseIdentifier:)初始化程序,除非你要为它添加一些初始化。

如果您计划为其添加一些初始化,请注意故事板中的原型单元格不会init(style:reuseIdentifier:) 它们由init(coder:)初始化。

如果您将故事板与原型单元格一起使用,则不确定为什么需要自定义UITableViewCell类。 您可以将标签和文本视图放入单元格并使用它们。

如果您使用xib工作,那么我会得到它,但您只需要:

class commentCell: UITableViewCell {
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var commentLabel: UITextView!

}

然后,您将在TableView类中注册xib:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerNib(UINib(nibName: "commentCell", bundle: nil),
            forCellReuseIdentifier: "reuseIdentifier")
    }

关于cellForRowAtIndexPath函数,语法现在有点修改:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

 var cell = self.tableView.dequeueReusableCellWithIdentifier("prototypeCell") as commentCell



        return cell
    }

如果您想发布到github,我们可以帮助您进行修改。 没有看到更多代码就很难具体。

在Swift 4中继承UITableViewCell的正确方法:

class MyTableViewCell: UITableViewCell
{    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }
}

斯威夫特4

按照建议实现required init ,并在要添加的自定义初始化程序中添加super.init(nibName: nil, bundle: nil)

例如:

init(input: ProtocolType? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.input = input
}

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

暂无
暂无

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

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