的UITableViewCell,不能援引“初始化”与类型的参数列表“(编码器:NSCoder)”
所以香港专业教育学院建立了一个自定义单元格类我UITableView
。 Xcode中坚持,这UITableViewCell
有以下初始化(Xcode中实际上自动填写该代码对我来说):的UITableViewCell,不能援引“初始化”与类型的参数列表“(编码器:NSCoder)”
class customCell: UITableViewCell {
required init(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
伊夫修改这个初始化的内容包含以下内容:
// fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
如果我删除。required
然后Xcode中踢了一个错误,并希望通过将关键词早在纠正我的关键字
所以在我UITableView
类我尝试注册该小区像这样:
override func viewDidLoad(){
super.viewDidLoad()
let coder: NSCoder = NSCoder()
let customCell: AnyClass = customCell(coder: coder)
table?.registerClass(customCell, forCellReuseIdentifier:"cell")
倒数第二行(就是我初始化customCell与NSCoder,如Xcode中要我)产生错误:
Cannot invoke 'init' with an argument list of type '(coder: NSCoder)'
如果我能我更愿意建立自己的初始化没有NSCoder。
如果我尝试使用其他任何初始化然后Xcode中踢了一个错误的线沿线的:
Extra argument 'reuseIndenifier' in call
或
Missing argument in call
它将继续抱怨,直到我使用的初始化与NSCoder 。我被迫使用这个初始化器。
请纠正我,如果我在这里的方式,但对我来说,好像我陷入了catch 22的情况 - Xcode抱怨,如果我建立一个没有编码器的初始化器,然后当我尝试使用初始化与它抱怨我使用的是编码器。
尽管存在这种明显的矛盾,但我相当确信这一切都是我在某个地方成为笨拙的结果。
有没有人有足够的指点我在正确的方向?
您需要注册class
本身不是它的一个实例。
例如。
tableView?.registerClass(customCell.self, forCellReuseIdentifier:"cell")
的问题与需要init(coder aDecoder:NSCoder)
是一个单独的一个,在这里讨论:'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`
非常感谢您的帮助! – Jimmery 2014-11-25 17:03:29
你看了UITableView的编程指南? afaik这不是你如何使用UITableView和UITableViewCell的,但我可能是错的。 initWithCoder初始化程序在使用故事板时被调用,并且不应该发生,因为您通常会在UITableViewDataSource委托方法中初始化UITableViewCell对象。所以也许有更多的背景会对此有所帮助。 – believesInSanta 2014-11-25 11:53:38
我试图创建一个自定义'UITableViewCell'编程 - 请这样的问题:http://stackoverflow.com/questions/27103278/creating-a-uitableviewcell-programmatically-in-swift – Jimmery 2014-11-25 11:55:00
你可以尝试使用'的init(风格样式:UITableViewCellStyle, reuseIdentifier reuseIdentifier:String?),就像我说的,initWithCoder被接口生成器 – believesInSanta 2014-11-25 12:01:29