[英]How to subclass custom Cell class with xib in swift
我遇到了簡單的問題,無法找到任何解決方案。
MyCell.xib
有文件MyCell : UITableViewCell
類。
我這樣用它:
viewDidLoad方法:
let nib = UINib(nibName: "MyCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "myIdentifier")
tableView cellForRowAt方法:
let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath) as? MyCell
它運作良好。
我將我的類子類化為添加一些新方法:
class SuperCell : MyCell {
func coolMethod {
print("cool")
}
}
並嘗試使用它:
let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath) as? SuperCell
它返回nil
我怎樣才能使它工作?
我嘗試在InterfaceBuilder中使用標識符myIdentifier
和類SuperCell
創建原型單元格,但它沒有幫助。
我只想對不同的單元類使用相同的視圖(xib)。
在我來說,我有共同的細胞( MyCell
與視圖(廈門國際銀行))。 MyCell
完全描述了字段(IBOutlets)。 此外,我想創建一些子類MyCell
另一個單元類,但它們將提供這些IBOutlet的一些行為。 例如FirstMyCell : MyCell
將有方法setFieldsFrom(objectOne: ObjectOne)
和SecondMyCell : MyCell
將有另一個方法setFieldsFrom(anotherObject: ObjectAnother)
。
當然,我可以將這兩個方法添加到我的MyCell
類中,但它將是不潔凈的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.