问了类似的问题here但这并不能为我解决这个问题。我添加了 tableView
在ViewController
.使用它的数据源和委托(delegate)扩展类并为其添加所需的方法。
然后我在此表中创建了一个原型(prototype)单元格(不是单独的 .xib)并为此创建了一个类 TableViewCell
并收集了 @IBOutlet
:
@IBOutlet weak var titleOfAccount: UILabel!
@IBOutlet weak var lastModified: UILabel!
@IBOutlet weak var accountImage: UIImageView!
@IBOutlet weak var cellView: UIView!
然后在cellForRowAt
我写的时候tableview的方法
cell.titleOfAccount.text = "Hello World"
cell.lastModified.text = "Last Modified: 21 May 2017"
并运行因该错误而崩溃的代码。
fatal error: unexpectedly found nil while unwrapping an Optional value
我在这里和那里搜索后做了什么我回到了tableViewCell
类(class)和改变!
至 ?
并更新了 cellForRowAt
中的代码作为:
cell.titleOfAccount?.text = "Hello World"
cell.lastModified?.text = "Last Modified: 21 May 2017"
现在,当我运行程序时,它运行成功,但是 tableView
中没有出现任何单元格。我还实现了这个方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3;
}
这是我的 cellForRowAt
的完整代码方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: AccountsTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! AccountsTableViewCell
cell.backgroundColor = UIColor.clear
cell.clipsToBounds = true
cell.cellView.layer.shadowColor = UIColor.black.cgColor
cell.cellView.layer.shadowOpacity = 1
cell.cellView.layer.shadowOffset = CGSize.zero
cell.cellView.layer.shadowRadius = 10
cell.titleOfAccount.text = "Hello World"
cell.lastModified.text = "Last Modified: 21 May 2017"
cell.accountImage.image = UIImage(named: "fb")
return cell
}
附言我还添加了 self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
在 viewDidLoad
最佳答案
从您的 viewDidLoad()
中删除以下行(代码),因为您已经从 Storyboard连接了您的 Cell。
self.tableView.register(AccountsTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
同时分享身份检查器的快照(属性检查器的左侧)。
关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value in UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551133/