ios - fatal error : unexpectedly found nil while unwrapping an Optional value in UITableViewCell

标签 ios swift uitableview swift3

问了类似的问题here但这并不能为我解决这个问题。我添加了 tableViewViewController .使用它的数据源和委托(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

enter image description here enter image description here

最佳答案

从您的 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/

相关文章:

ios - 大小为零的 UIView 对象是否占用大量内存?

python - Swift 异常处理 vs python 异常

ios - 如何在 Swift 4 中将 Any 转换为 Date?

ios - UISearchBar 在点击时消失

ios - 将图像保存为 nsdata 到 plist 并接收内存警告

ios - 在ARKit 1.5中进行初始检测后如何跟踪图像 anchor ?

cocoa-touch - UIModalPresentationFormSheet 白角

ios - 根据 UILabel 内容调整 UITableViewCell 高度

ios - 有没有比这个更好的解决 UITableview 单元格圆角的方法?

ios - 滚动 collectionview 时数据已被替换或丢失