当我尝试修改单元格 subview 时,UITableViewCell 因“隐式展开可选值时意外发现 nil”而崩溃。
注册tableView单元格
self.tableView.register(TravelTypeTableViewCell.self, forCellReuseIdentifier: "TravelCell")
tablView方法
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 224
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TravelCell", for: indexPath) as! TravelTypeTableViewCell
cell.travelTypeSegment.selectedSegmentIndex = 0
return cell
}
TableViewcell 类
class TravelTypeTableViewCell: UITableViewCell {
@IBOutlet var travelTypeSegment: UISegmentedControl!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Storyboard图像
最佳答案
评论此内容
self.tableView.register(TravelTypeTableViewCell.self, forCellReuseIdentifier: "TravelCell")
对于原型(prototype)单元,您不应该注册它们,因为这会自动为它们完成,并确保 travelTypeSegment
在 IB 中连接
关于ios - UITableView 原型(prototype)单元崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59649464/