类似的问题被问到
here,但这并不能解决这个问题.我在ViewController中添加了一个tableView.使用它的dataSource和Delegate扩展了类,并为它添加了所需的方法.然后我在此表中创建了一个原型单元格(不是单独的.xib),并为该TableViewCell创建了一个类并收集了@IBOutlet:
@IBOutlet weak var titleOfAccount: UILabel! @IBOutlet weak var lastModified: UILabel! @IBOutlet weak var accountImage: UIImageView! @IBOutlet weak var cellView: UIView!
然后在我写的表视图的cellForRowAt方法中
cell.titleOfAccount.text = "Hello World" cell.lastModified.text = "Last Modified: 21 May 2017"
并运行它与此错误崩溃的代码.
fatal error: unexpectedly found nil while unwrapping an Optional value
我在这里和那里搜索后做了什么,我回到tableViewCell类并改变了!至 ?并将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 }
附:我还在viewDidLoad中添加了self.tableView.register(AccountsTableViewCell.self,forCellReuseIdentifier:cellReuseIdentifier)
解决方法
从viewDidLoad()中删除以下行(代码),因为您已从故事板连接了Cell.
self.tableView.register(AccountsTableViewCell.self,forCellReuseIdentifier: cellReuseIdentifier)
还为身份检查器共享快照(仅在属性检查器的左侧).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。