[英]Create a customized table view cell using swift
我正在按照本教程http://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views創建自定義表格單元格。 但我使用的是 swift 而不是目標 c。
這是我在 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)
}
}
但它不編譯,我收到錯誤消息:
MyTableViewCell.swift:38:9:屬性“self.delegate”未在 super.init 調用時初始化
如果我去掉“init(coder aDecoder: NSCoder)”,我會收到錯誤提示“必需的”初始值設定項“init(coder:)”必須由 UITableViewCell 的子類提供。
我讀過這個線程Class does not implement its superclass's required members
關於在我需要的 init(coder aDecoder: NSCoder) 中做什么。
但是我無法編譯代碼。
如果有人可以幫助我,我將不勝感激。
謝謝你。
有 3 種方法可以解決您的問題,各有各的優點和麻煩。
如果您只需要編譯代碼,但永遠不會使用編碼器所需的初始化,則可以將默認錯誤保留在那里。
您也可以將委托字段設置為可選,但從現在開始,您需要處理代碼中 nil 的理論上的可能性。 像這樣:
var 委托:SwipeableCellDelegate?
guard let dlgt = delegate else {print("Delegate is not set for this cell"); 返回}
或者您可以在公共某處為委托定義默認值,並將其設置在所需的 init 中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.