코드 사용자 정의 Cell(사용자 정의 Cell이 제대로 표시되지 않음 및 fatal error:unexpectedly found nil)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 。
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIDForCodeCustomCell", for: indexPath) as! CodeCustomCell
// Configure the cell...
cell.configure(modelData: (modelData?[indexPath.row])!) // fatal error: unexpectedly found nil while unwrapping an Optional value
return cell
}
두 가지 오류:
1. fatal error: unexpectedly found nil while unwrapping an Optional value( )
2. cell ,View cell
위의 코드가 정상적으로 작동하지 않습니다.알고 있는 것은:
UItable ViewCell을 다시 쓰기 위한 prepare ForReuse를 참조하십시오.
cell을 다시 사용하는 메커니즘은 버퍼를 이용하여 다시 사용할 수 있는cell을 저장하고cell을 표시할 때 먼저 버퍼에서 꺼내는 것이다. 만약에 버퍼에 이런cell이 없다면 다시 사용할 수 있는cell이 없으면 이때cell을 다시 초기화하고 버퍼에 추가한다.
다음과 같은 몇 가지 문제가 해결되었습니다.
수정된 코드를 붙이다.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CodeCustomCell.self, forCellReuseIdentifier: "CellIDForCodeCustomCell")
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIDForCodeCustomCell", for: indexPath) as UITableViewCell
// Configure the cell...
(cell as! CodeCustomCell).configure(modelData: (modelData![indexPath.row])!)
return cell
}
class CodeCustomCell: UITableViewCell{
var _imageView:UIImageView!
var label1:UILabel!
var label2:UILabel!
var label3:UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
_imageView = UIImageView(frame: CGRect(x: 50, y: 0, width: 88, height: 88))
label1 = UILabel(frame: CGRect(x: 0, y: 0, width: 42, height: 21))
label2 = UILabel(frame: CGRect(x: 0, y: 29, width: 42, height: 21))
label3 = UILabel(frame: CGRect(x: 0, y: 58, width: 42, height: 21))
}
func configure(modelData:ModelData){
label1.text = modelData.attr1
label2.text = modelData.attr2
label3.text = modelData.attr3
_imageView.image = UIImage(named: modelData.imageName)
self.addSubview(label1)
self.addSubview(label2)
self.addSubview(label3)
self.addSubview(_imageView)
}
required init?(coder aDecoder: NSCoder) {
// super.init(style: .default, reuseIdentifier: "CellIDForCodeCustomCell")
fatalError("init(coder:) has not been implemented")
}
}
그리고 정상적으로 나타나지 못한 다른 원인도 말씀드리겠습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.