回答編集履歴
3
追記
test
CHANGED
@@ -181,3 +181,7 @@
|
|
181
181
|
}
|
182
182
|
|
183
183
|
```
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
選択されているセルのインデックスパスは、[indexPathsForSelectedItems](https://developer.apple.com/documentation/uikit/uicollectionview/1618099-indexpathsforselecteditems)とかでとれます
|
2
誤字訂正
test
CHANGED
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
|
72
72
|
|
73
|
-
// 選択の
|
73
|
+
// 選択の条件を設定する
|
74
74
|
|
75
75
|
//collectionView.allowsSelection = true
|
76
76
|
|
@@ -101,8 +101,6 @@
|
|
101
101
|
cell.label.text = String(format: "%02d", indexPath.row)
|
102
102
|
|
103
103
|
// ここで背景を設定するだけ
|
104
|
-
|
105
|
-
|
106
104
|
|
107
105
|
cell.selectedBackgroundView = CellBgView(num: indexPath.row)
|
108
106
|
|
1
編集
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
![イメージ説明](49
|
1
|
+
![イメージ説明](7df4d9ba780a4841380bfcc170ed15ca.gif)
|
2
2
|
|
3
3
|
|
4
4
|
|
@@ -8,4 +8,178 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
|
11
|
+
私はこの方法が良いと思います。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
```swift
|
16
|
+
|
17
|
+
import UIKit
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
// 背景用のView
|
22
|
+
|
23
|
+
class CellBgView: UIView {
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
init(num: Int) {
|
28
|
+
|
29
|
+
super.init(frame: CGRect.zero)
|
30
|
+
|
31
|
+
// 偶数と奇数で色変えてみよう
|
32
|
+
|
33
|
+
backgroundColor = num % 2 == 0 ? .red : .orange
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
required init(coder aDecoder: NSCoder) {
|
40
|
+
|
41
|
+
fatalError("init(coder:) has not been implemented")
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
class ViewController: UIViewController{
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
var array = [Int](0...30)
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
@IBOutlet weak var collectionView: UICollectionView!
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
private let sectionInsets = UIEdgeInsets(top: 20.0, left: 10.0, bottom: 20.0, right: 10.0)
|
62
|
+
|
63
|
+
private let itemsPerRow: CGFloat = 3
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
override func viewDidLoad() {
|
68
|
+
|
69
|
+
super.viewDidLoad()
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// 選択の情景を設定する
|
74
|
+
|
75
|
+
//collectionView.allowsSelection = true
|
76
|
+
|
77
|
+
collectionView.allowsMultipleSelection = true
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
extension ViewController: UICollectionViewDataSource {
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
90
|
+
|
91
|
+
return array.count
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
98
|
+
|
99
|
+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
|
100
|
+
|
101
|
+
cell.label.text = String(format: "%02d", indexPath.row)
|
102
|
+
|
103
|
+
// ここで背景を設定するだけ
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
cell.selectedBackgroundView = CellBgView(num: indexPath.row)
|
108
|
+
|
109
|
+
return cell
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
class CustomCell: UICollectionViewCell {
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
@IBOutlet weak var label: UILabel!
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
// あとはレイアウト用です
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
extension ViewController: UICollectionViewDelegateFlowLayout {
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
|
146
|
+
|
147
|
+
let availableWidth = UIScreen.main.bounds.width - paddingSpace
|
148
|
+
|
149
|
+
let withPerItem = availableWidth / itemsPerRow
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
//cellの大きさを返す
|
154
|
+
|
155
|
+
return CGSize(width: withPerItem, height: withPerItem)
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
return sectionInsets
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
return sectionInsets.left
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
```
|