teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

書式の改善

2021/04/02 21:50

投稿

pi.chan_san
pi.chan_san

スコア3

title CHANGED
@@ -1,1 +1,1 @@
1
- CollectionViewを選択した時、imageviewの色合いを変えたい
1
+ 【Swift】CollectionViewを選択した時、imageviewの色合いを変えたい
body CHANGED
File without changes

3

書式の改善

2021/04/02 21:50

投稿

pi.chan_san
pi.chan_san

スコア3

title CHANGED
File without changes
body CHANGED
@@ -104,8 +104,6 @@
104
104
  override func viewDidLoad() {
105
105
  super.viewDidLoad()
106
106
  self.pageScrollView.delegate = self
107
- //navigationbarの下からのy座標になる。bar系が全部グレーになってしまったのでやめました。
108
- // edgesForExtendedLayout = []
109
107
 
110
108
  //生成したcollectionViewのdataSourceとdelegteを紐づける
111
109
  collectionView.dataSource = self
@@ -158,7 +156,6 @@
158
156
 
159
157
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
160
158
 
161
- //ここでは画面の横サイズの半分の大きさのcellサイズを指定
162
159
  return CGSize(width: (screenSize.width-50) / 2, height: (screenSize.width-50)/2 + 50)
163
160
  }
164
161
 

2

情報の追加

2021/04/02 13:48

投稿

pi.chan_san
pi.chan_san

スコア3

title CHANGED
File without changes
body CHANGED
@@ -133,6 +133,17 @@
133
133
 
134
134
  self.view.endEditing(true)
135
135
  }
136
+   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
137
+
138
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
139
+
140
+ let imagepath = imageList[indexPath.item]
141
+ let cellText = recipeList[indexPath.item]
142
+ cell.setupContents(textName: cellText, imagepath: imagepath)
143
+
144
+ return cell
145
+ }
146
+
136
147
  //イベントの設定
137
148
  extension MainChooseViewController: UICollectionViewDelegate {
138
149
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

1

書式の改善

2021/04/02 13:47

投稿

pi.chan_san
pi.chan_san

スコア3

title CHANGED
File without changes
body CHANGED
@@ -76,19 +76,97 @@
76
76
  }
77
77
  }
78
78
  ```
79
+ ```Swift
80
+ import UIKit
79
81
 
82
+ class MainChooseViewController: UIViewController {
83
+
84
+ //以下のdictionaryは、collectionviewの中身です
85
+ var recipeList = ["pasta", "omelette rice", "Yellowtail teriyaki", "scallop cooked rice"]
86
+ var imageList = ["3ac4720cd39fcb7bc418a360734f4769f593c4e0.jpg", "26fb8d743b3e3e7645a4a947f4db74cb.jpg.webp", "i=https%3A%2F%2Fimage.excite.co.jp%2Fjp%2Ferecipe%2Frecipe%2F0%2F0%2F0075f14723f0ca3a9bcf5062871299b4%2F8e6ae4fb7223467b78bfd88ab8559dc0.jpeg&small=400&quality=100&type=jpeg.jpeg", "recipe.jpg"]
87
+ private let collectionView: UICollectionView = {
88
+
89
+ //セルのレイアウト設計
90
+ let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
91
+
92
+ //各々の設計に合わせて調整
93
+ layout.scrollDirection = .vertical
94
+ layout.minimumInteritemSpacing = 0
95
+ layout.minimumLineSpacing = 10
96
+
97
+ let collectionView = UICollectionView( frame: CGRect(x: 20, y: 100, width: screenSize.width - 40, height: screenSize.height/2 + 50 ), collectionViewLayout: layout)
98
+ collectionView.backgroundColor = UIColor.white
99
+ //セルの登録
100
+ collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
101
+ return collectionView
102
+ }()
103
+
104
+ override func viewDidLoad() {
105
+ super.viewDidLoad()
106
+ self.pageScrollView.delegate = self
107
+ //navigationbarの下からのy座標になる。bar系が全部グレーになってしまったのでやめました。
108
+ // edgesForExtendedLayout = []
109
+
110
+ //生成したcollectionViewのdataSourceとdelegteを紐づける
111
+ collectionView.dataSource = self
112
+ collectionView.delegate = self
113
+
114
+
115
+ view.addSubview(collectionView)
116
+ mainView.addSubview(collectionView)
117
+
118
+ if pageControl.currentPage == 0 {
119
+ backButton.isHidden = true
120
+ } else {
121
+ backButton.isHidden = false
122
+ }
123
+
124
+ if pageControl.currentPage == 2 {
125
+ nextButton.isHidden = true
126
+ }
127
+
128
+ }
129
+
130
+
131
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
132
+ super.touchesBegan(touches, with: event)
133
+
134
+ self.view.endEditing(true)
135
+ }
136
+ //イベントの設定
137
+ extension MainChooseViewController: UICollectionViewDelegate {
138
+ func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
139
+ print("callされました")
140
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
141
+ cell.onTapped(true)
142
+ }
143
+ }
144
+
145
+ //cellのサイズの設定
146
+ extension MainChooseViewController: UICollectionViewDelegateFlowLayout {
147
+
148
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
149
+
150
+ //ここでは画面の横サイズの半分の大きさのcellサイズを指定
151
+ return CGSize(width: (screenSize.width-50) / 2, height: (screenSize.width-50)/2 + 50)
152
+ }
153
+
154
+ }
155
+ ```
156
+
80
157
  ### 試したこと
81
158
 
82
159
  試しに色々なところをコメントアウトしてみたのですが、
83
160
  `ciFilter.setValue(70, forKey: kCIInputContrastKey)`
84
161
  の部分をコメントアウトしてみた時、画面遷移はすることができるようになりました。
85
162
  その時、collectionViewを試しに選択してみたら、
86
- onTapped関数内の
163
+ cellクラス、onTapped関数内の
87
164
  `chooseImage.image = UIImage(ciImage: filteredImage!)`の部分に
88
165
 
89
166
  `Fatal error: Unexpectedly found nil while unwrapping an Optional value`
90
- このようなエラーが出ました。この原因はなんとなく想像できます。
167
+ このようなエラーが出ました。printの"callされました"は呼び出されていて、このエラーの原因はなんとなく想像できます。
91
168
  情報の一つとして参考にしていただけたら嬉しいです。
169
+ わかる方、お力添えよろしくお願いします。
92
170
 
93
171
  ### 補足情報(FW/ツールのバージョンなど)
94
172