UITableVIewの中にUICollectionViewを入れたいのですが、
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
とエラーがでます。なぜでしょうか、また解決策はなんでしょうか。
ご教授お願い致します。
現在のコード(全てコードで書いています)
TableViewCell
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
class TableCell: UITableViewCell {
var collection = UICollectionView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
}
override func layoutSubviews() {
super.layoutSubviews()
self.addSubview(collection)
}
func register(_ cellClass: AnyClass?,forCellReuseIdentifier identifier: String){
}
private func setup(){
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: width/3, height: width/3)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: width, height: height), collectionViewLayout: layout)
collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collection.backgroundColor = .white
collection.dataSource = self
collection.delegate = self
}
}
extension TableCell: UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .cyan
return cell
}
}
TableView
class TableVC: UIViewController {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.setup()
}
}
extension TableVC: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
extension ProfileVC{
private func setup(){
self.navigationItem.title = "www"
self.setupTableView()
self.view.addSubview(tableView)
}
private func setupTableView(){
tableView = UITableView(frame: self.view.frame, style: .plain)
tableView.register(TableCell.classForCoder(), forCellReuseIdentifier: "TableCell")
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.separatorStyle = .none
tableView.allowsSelection = false
}
}