【问题标题】:IBOutlet: Unexpectedly found nil while implicitly unwrapping an Optional valueIBOutlet:在隐式展开可选值时意外发现 nil
【发布时间】:2019-05-21 05:53:21
【问题描述】:

我使用 IBOutlet 将 TableView 正确连接到 CollectionViewCell,但是当我尝试使用此 tableview 执行某些操作时,例如:cell.tableview.tag = x,它显示:

在隐式展开 Optional 值时意外发现 nil。 当我取消注释时

import UIKit

class CollectionCell: UICollectionViewCell {
    // When I uncomment the next line, it works good
    //let tableView = UITableView()



    @IBOutlet var tableView: UITableView!
}

class ViewController: UIViewController {

    var testArray = [["Day0-0","Day0-1","Day0-2"], ["Day1-0","Day1-1","Day1-2"], ["Day2-0","Day2-1","Day2-2"],["Day3-0","Day3-1","Day3-2"],["Day4-0","Day4-1","Day4-2"]]

    @IBOutlet weak var collectionView: UICollectionView!
    // let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white


        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionCell")
        view.addSubview(collectionView)
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[collectionView]|", options: [], metrics: nil, views: ["collectionView":collectionView]))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[collectionView]|", options: [], metrics: nil, views: ["collectionView":collectionView]))

    }
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return testArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        // Here is where i get the error: "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
        cell.tableView.tag = indexPath.item
        cell.tableView.reloadData()
        cell.tableView.delegate = self
        cell.tableView.dataSource = self
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.frame.size
    }
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testArray[tableView.tag].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
        cell.textLabel?.text = testArray[tableView.tag][indexPath.row]
        return cell
    }
}

【问题讨论】:

  • 你的 tableview 单元笔尖文件似乎还没有准备好

标签: ios swift uitableview


【解决方案1】:

从您的 viewDidLoad 方法中删除这一行。

collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionCell")

它用于在集合视图中注册以编程方式创建的集合视图单元格。 如果在情节提要中拥有所有内容时使用此方法,它将创建一个其 IBOutlets 为 nil 的单元格。

【讨论】:

  • 如何填写??
  • @mohsen 你想做什么?请解释
  • 我想初始化出口对象,因为它们是 nil,我只是把 ?在他们之后,它奏效了。感谢您的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
  • 2016-10-18
相关资源
最近更新 更多