首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IBOutlet:在隐式展开可选值时意外地发现为零

IBOutlet:在隐式展开可选值时意外地发现为零
EN

Stack Overflow用户
提问于 2019-05-21 05:53:21
回答 1查看 5.5K关注 0票数 3

我正确地将tableview与CollectionViewCell连接到了一个IBOutlet,但是当我尝试使用这个表视图做一些事情时,例如:cell.tableview.tag = x,它说:

在隐式展开可选值时意外地找到了nil。当我取消评论的时候

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-21 06:01:15

从viewDidLoad方法中删除这一行。

代码语言:javascript
复制
collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionCell")

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

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56231865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档