我已使用IBOutlet将TableView正确连接到CollectionViewCell上,但是当我尝试使用此Tableview执行某些操作时,例如:cell.tableview.tag = x,它说:
意外找到nil,同时隐式展开Optional值。当我取消评论
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])) …Run Code Online (Sandbox Code Playgroud)