我正在使用 tableView
并尝试创建我的 cells
..徒劳。
我设置了我们必须做的所有事情(创建了一个原型(prototype)单元,给出了一个标识符(“CustomerCell”),在 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):
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”?
这可以在身份检查器中完成。
关于ios - UITableViewCell 初始化失败 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348160/