xcode 必须调用超类“UITableViewCell”的指定初始值设定项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46097450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:13:31  来源:igfitidea点击:

Must call a designated initializer of the superclass 'UITableViewCell'

iosxcodeuitableviewswift3

提问by Sri Vathsav

   let bubbleView : UIView = {

    let view = UIView()
    view.backgroundColor = blueColor
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 16
    view.layer.masksToBounds = true
    return view
}()


let messageImageView : UIImageView = {

    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.layer.cornerRadius = 16
    imageView.layer.masksToBounds = true
    imageView.contentMode = .scaleAspectFill
    return imageView

}()
init(frame: CGRect) {

    super.init(frame: frame)
}

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

// getting error in like "super.init(frame: frame)" as Must call a designated initializer of the superclass 'UITableViewCell'please help me in sorting this problem thanks in advance...

// 像“super.init(frame: frame)”一样出现错误必须调用超类'UITableViewCell'的指定初始值设定项,请帮我解决这个问题,提前谢谢...

回答by kamwysoc

I guess the code that you provided is from UITableViewCelltype class. So in the initializer you should call designed initializer for this class. Not from UIView

我猜您提供的代码来自UITableViewCell类型类。因此,在初始化程序中,您应该为此类调用设计的初始化程序。不是来自UIView

The designated initializer for UITableViewCellclass is

UITableViewCell类的指定初始值设定项是

init(style: UITableViewCellStyle, reuseIdentifier: String?)

init(style: UITableViewCellStyle, reuseIdentifier: String?)

So in you class you should override this initializers:

因此,在您的课程中,您应该覆盖此初始化程序:

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

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

回答by Phillip Mills

From the docs for init(style: UITableViewCellStyle, reuseIdentifier: String?):

从文档中init(style: UITableViewCellStyle, reuseIdentifier: String?)

This method is the designated initializer for the class.

此方法是该类的指定初始值设定项。

The superinitializer you're calling is for UIView, not UITableViewCell.

super您正在调用的初始化程序是用于UIView,而不是UITableViewCell