【问题标题】:UITableView inside UICollectionView -> [error] unexpectedly found nil while unwrapping tableViewUICollectionView 内的 UITableView -> [错误] 在展开 tableView 时意外发现 nil
【发布时间】:2017-04-10 09:33:38
【问题描述】:

我在 UICollectionView 的每个单元格内都有一个 UITableView。

我将表格视图引用到 FeedCell 类,它是集合视图中每个单元格的类。但是,我收到一个错误提示

fatal error: unexpectedly found nil while unwrapping an Optional value

尝试访问 FeedCell 类中的 tableView 对象时。我三次检查我是否正确地从情节提要中引用了表格视图,所以我假设还有其他原因导致它为零,但我不确定它是什么。有没有人知道如何解决这个问题?

这是我的 VC,集合视图和表格视图都在其中。

class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.isTranslucent = false
        collectionView.register(FeedCell.self, forCellWithReuseIdentifier: "FeedCell")
    }

    // for feedcell and its collection view
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeedCell", for: indexPath) as! FeedCell
        cell.backgroundColor = backgrounds[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        guard let collectionViewCell = cell as? FeedCell else { return }

        collectionViewCell.setTableViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row)
    }


    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: view.frame.width, height: view.frame.height)
    }

    // conform to tableview protocol for hometable
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return images[tableView.tag].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath)
        cell.imageView?.image = images[tableView.tag][indexPath.item]
        return cell
    }


    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        super.viewWillAppear(animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
        super.viewWillDisappear(animated)
    }


}

这里是集合视图的一个单元格,我在其中引用了表格视图。

class FeedCell: BaseCollectionViewCell{

    @IBOutlet weak var tableView: UITableView!
    override func setupViews() {
        super.setupViews()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

}


extension FeedCell {

    func setTableViewDataSourceDelegate
        <D: UITableViewDataSource & UITableViewDelegate>
        (dataSourceDelegate: D, forRow row: Int) {

        # ERROR!
        tableView.delegate = dataSourceDelegate
        tableView.dataSource = dataSourceDelegate
        tableView.tag = row
        tableView.reloadData()
    }

}

【问题讨论】:

  • 你的错误在哪一行?
  • 这是行“tableView.delegate = dataSourceDelegate”。我在错误行之前添加了一条评论。
  • 只需删除该行 tableView.delegate = dataSourceDelegate tableView.dataSource = dataSourceDelegate 并从情节提要中放置代表并且其工作正常
  • 我试过了,但我不能把委托和数据源放到 FeedCell 中,因为它是集合视图单元格的子类。我只能将委托和数据源指向 Storyboard 中的集合视图。

标签: ios swift uitableview uicollectionview storyboard


【解决方案1】:

在这一行:

 (dataSourceDelegate: D, forRow row: Int)

您将其转换为变量:D

改用这个:

tableView.dataSource = D
tableView.delegate = D

【讨论】:

  • tableView 对象的错误是fatal error: unexpectedly found nil while unwrapping an Optional value f。因此,使用您的解决方案,tableView 对象仍然为零
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多