[英]Initialization of UITableViewCell Fails (Swift)
我正在使用tableView
並嘗試創建我的cells
。 我設置了我們要做的所有事情(創建了一個原型單元,提供了一個標識符(“ CustomerCell”),在storyBoard
設置了delegate
和dataSource
storyBoard
並在ViewDidLoad
都設置了,在StoryBoard
設置了tableView
的好類, cells
等)。
這是我的代碼:
override func viewDidLoad() {
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
switch item.type {
case .customer:
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomerCell", for: indexPath) as? CustomerCellSetter {
cell.item = item as? Customer // THIS is never called, the cell return nil all the time
return cell
}
return UITableViewCell()
}
方程中是否還有其他參數可以獲取我的單元格? 在此先感謝您的寶貴幫助!
編輯:
這是我的UITableViewCell
類:
class CustomerCellSetter: CustomerTableViewCell {
var item: Customer? {
didSet {
guard let item = item else {
return }
if let firstName = item.firstName {
fisrtName?.text = firstName
}
if let theLastName = item.lastName {
lastName.text = theLastName
}
if let theGsm = item.GSM {
gsm.text = theGsm
}
if let theMail = item.mail {
mail.text = theMail
}
if let theAdress = item.adress {
adress.text = theAdress
}
if let theNote = item.notes {
notes.text = theNote
}
}
}
}
class CustomerTableViewCell: UITableViewCell {
@IBOutlet var fisrtName : UILabel!
@IBOutlet var lastName : UILabel!
@IBOutlet var gsm : UILabel!
@IBOutlet var mail : UILabel!
@IBOutlet var adress : UILabel!
@IBOutlet var notes : UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
編輯2:
您是否將單元格的類別設置為“ CustomerCellSetter”?
這可以在身份檢查器中完成。
您是否在類名中添加了“ UITableViewDelegate,UITableViewDataSource”?
class ClassName: UITableViewDelegate, UITableViewDataSource {
}
而且您還需要添加更多tableview方法
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // number of your rows
}
我需要在身份檢查器中設置CustomCellSetter,而不是CustomerTableViewCell。 感謝@OOPer這個簡單的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.