0
我创建了一个UITableView
,这是我的代码吧:自定义的UITableViewCell与零的UIImageView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
// let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "SubjectCell")
let cell = SubjectCell(style: UITableViewCellStyle.default, reuseIdentifier: "SubjectCell")
cell.cellUIImage.image = UIImage(named: "menu.png")
// cell.cellUIImage = UIImageView(UIImage(named: "menu-32.png"))
if indexPath.row % 2 == 0{
cell.backgroundColor = UIColor.cyan
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
而且我SubjectCell
类如下:
import Foundation
import UIKit
class SubjectCell : UITableViewCell{
@IBOutlet var cellUIImage: UIImageView!
}
然而,当我运行此代码,它崩溃。调试,我注意到cellUIImage是零,所以它解开它时崩溃。 IBOulet
链接到我的原型单元格。
解缠的是由Xcode中完成的,它不应该是零,因为它实际上是细胞UIImageView
。
为什么不正确地从表格视图中取出单元格? – rmaddy
@rmaddy特别没有理由。这是一个非常原始的代码,我通常会在之后对它进行裁剪......但是,我倒下了这不是它崩溃的原因。 –
这正是它失败的原因。你的代码不会从你的xib/storyboard创建一个单元格。你只是从代码创建你的单元格。因此没有设置单元的出口。正确地创建你的细胞。正确注册单元类/笔尖,它将按预期工作。 – rmaddy