CollectionViewをコードだけで表示したい
iOSアプリ開発初心者です。
現在、StoryBoardを使わずコードだけでアプリ開発を行っています。
アプリを開いた後、最初に表示されるViewControllerに、UICollectionViewを表示したいのですがアプリを開くとすぐにエラーが出てしまいます。
delegateもdatasourceもしっかりと設定し、layoutも設定済みです。
様々なサイトで調べましたが、解決策が未だ見つかっていません。
何卒、ご教授お願いいたします。
発生している問題・エラーメッセージ
デリゲートファイルには以下のエラーが表示されています。
AppDelegate
1class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
アプリ展開後、最初に表示されるViewControllerには以下のエラーが出ています。
HomeView
12019-07-15 17:25:54.960710+0900 Picker[945:209204] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 2*** First throw call stack: 3(0x19ff963a8 0x19f19bd00 0x19fe9bbe4 0x1ccb21e94 0x10263d93c 0x102633c30 0x10263b3f8 0x10263b670 0x10263d8d0 0x102636598 0x10263e618 0x10263e980 0x1cd25f574 0x1cd260d44 0x1cd266764 0x1ccaf8e4c 0x1ccb01828 0x1ccaf8ab4 0x1ccaf93d0 0x1ccaf7618 0x1ccaf72d4 0x1ccafc004 0x1ccafce5c 0x1ccafbeb8 0x1ccb00ea8 0x1cd264ca0 0x1cce47408 0x1a2957fd0 0x1a296231c 0x1a2961a6c 0x1058a4778 0x1058a833c 0x1a299518c 0x1a2994e08 0x1a2995404 0x19ff26444 0x19ff263c0 0x19ff25c7c 0x19ff20950 0x19ff20254 0x1a215fd8c 0x1cd2684c0 0x10264050c 0x19f9dcfd8) 4libc++abi.dylib: terminating with uncaught exception of type NSException 5(lldb)
該当のソースコード
HomeView
1 2import UIKit 3import Firebase 4 5class HomeView: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource { 6 7 8 9 10 11 let ButtonTitle: [String] = ["Home","Account","Black Board","Schedule","Your Status","Option","Logout"] 12 13 14 15 var menuTab: UIView = UIView() 16 var MenuTable: UITableView = UITableView() 17 var shadow: UIView = UIView() 18 var InformationTable: UICollectionView = UICollectionView() 19 20 21 22 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 23 return ButtonTitle.count 24 } 25 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 26 let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(MenuTableCell.self), for: indexPath) as! MenuTableCell 27 cell.myLabel.text = ButtonTitle[indexPath.row] 28 return cell 29 } 30 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 31 self.MenuTable.setContentOffset(CGPoint(x: 0, y: -(MenuTable.contentInset.top)), animated: true) 32 let identifier: String = ButtonTitle[indexPath.row] 33 } 34 35 36 37 38 39 40 41 42 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 43 return ButtonTitle.count 44 } 45 46 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 47 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(InformationTableCell.self), for: indexPath) as! InformationTableCell 48 cell.backgroundColor = UIColor(red: CGFloat(drand48()), 49 green: CGFloat(drand48()), 50 blue: CGFloat(drand48()), 51 alpha: 1.0) 52 return cell 53 } 54 55 56 57 58 59 60 61 62 63 64 65 66 @objc func swipeLabel(_ sender: UISwipeGestureRecognizer) { 67 switch(sender.direction){ 68 case UISwipeGestureRecognizer.Direction.right: 69 UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseOut, animations: { 70 self.shadow.alpha = 0.7 71 self.menuTab.frame = CGRect(x: 0, y: ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!), width: (self.view.frame.width / 3) * 2, height: (self.view.frame.height - ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!))) 72 }, completion: nil) 73 break 74 case UISwipeGestureRecognizer.Direction.left: 75 if shadow.alpha > 0.5 { 76 UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseOut, animations: { 77 self.shadow.alpha = 0 78 self.menuTab.frame = CGRect(x: -((self.view.frame.width / 3) * 2), y: ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!), width: (self.view.frame.width / 3) * 2, height: (self.view.frame.height - ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!))) 79 }, completion: nil) 80 } else { 81 self.navigationController?.pushViewController(QRView(), animated: true) 82 } 83 break 84 default: break 85 } 86 } 87 88 89 override func viewDidLoad() { 90 super.viewDidLoad() 91 menuTab.frame = CGRect(x: -((self.view.frame.width / 3) * 2), y: ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!), width: (self.view.frame.width / 3) * 2, height: (self.view.frame.height - ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!))) 92 MenuTable.frame = CGRect(x: 0, y: 0, width: self.menuTab.frame.width, height: self.menuTab.frame.height - 100) 93 shadow.frame = CGRect(x: 0, y: ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!), width: self.view.frame.width, height: (self.view.frame.height - ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!))) 94 InformationTable.frame = CGRect(x: 0, y: ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!), width: self.view.frame.width, height: (self.view.frame.height - ((self.navigationController?.navigationBar.frame.height)! + (self.navigationController?.navigationBar.frame.minY)!))) 95 96 97 98 menuTab.backgroundColor = .white 99 self.view.addSubview(menuTab) 100 101 102 103 MenuTable.rowHeight = 70 104 MenuTable.delegate = self 105 MenuTable.dataSource = self 106 MenuTable.isScrollEnabled = false 107 MenuTable.separatorStyle = .none 108 MenuTable.register(MenuTableCell.self, forCellReuseIdentifier: NSStringFromClass(MenuTableCell.self)) 109 self.menuTab.addSubview(MenuTable) 110 111 112 113 shadow.backgroundColor = .black 114 shadow.alpha = 0 115 self.view.addSubview(shadow) 116 117 118 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 119 layout.scrollDirection = .vertical 120 layout.minimumInteritemSpacing = 0 121 layout.minimumLineSpacing = 0 122 InformationTable.collectionViewLayout = layout 123 InformationTable.backgroundColor = UIColor.white 124 InformationTable.register(InformationTableCell.self, forCellWithReuseIdentifier: NSStringFromClass(InformationTableCell.self)) 125 InformationTable.delegate = self 126 InformationTable.dataSource = self 127 self.view.addSubview(InformationTable) 128 129 130 self.title = "おかえりなさい" 131 self.view.backgroundColor = UIColor.white 132 self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "1559712904534137-2"), style: .done, target: self, action: #selector(addTapped(_:))) 133 self.navigationController?.navigationBar.tintColor = .black 134 self.view.bringSubviewToFront(menuTab) 135 } 136} 137 138 139class MenuTableCell: UITableViewCell { 140 var myLabel: UILabel = UILabel() 141 var separator: CALayer = CALayer() 142 required init?(coder aDecoder: NSCoder) { 143 super.init(coder: aDecoder) 144 } 145 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { 146 super.init(style: style, reuseIdentifier: reuseIdentifier) 147 myLabel.textColor = .black 148 myLabel.textAlignment = .left 149 myLabel.font = UIFont(name: "HiraginoSans-W3", size: 14) 150 self.addSubview(myLabel) 151 separator.backgroundColor = UIColor(red: 234/255, green: 234/255, blue: 234/255, alpha: 1).cgColor 152 self.layer.addSublayer(separator) 153 } 154 override func prepareForReuse() { 155 super.prepareForReuse() 156 } 157 override func layoutSubviews() { 158 super.layoutSubviews() 159 myLabel.frame = CGRect(x: 50, y: 0, width: self.frame.width, height: self.frame.height) 160 separator.frame = CGRect(x: myLabel.frame.minY, y: myLabel.frame.minY, width: myLabel.frame.width, height: 1) 161 } 162} 163 164 165 166final class InformationTableCell: UICollectionViewCell { 167 168 override init(frame: CGRect) { 169 super.init(frame: frame) 170 } 171 172 required init?(coder aDecoder: NSCoder) { 173 fatalError("init(coder:) has not been implemented") 174 } 175 176} 177 178 179 180
試したこと
・ColletionViewのLayout設定
・Podファイルのアップデート
補足情報(FW/ツールのバージョンなど)
Xcode(Version 10.2.1 (10E1001))
基本はFirebaseによって生成されるxcworkspaceで作業しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/15 09:14
2019/07/15 13:37
2019/07/15 14:38