回答編集履歴
3
追記
answer
CHANGED
@@ -89,4 +89,6 @@
|
|
89
89
|
|
90
90
|
}
|
91
91
|
}
|
92
|
-
```
|
92
|
+
```
|
93
|
+
|
94
|
+
選択されているセルのインデックスパスは、[indexPathsForSelectedItems](https://developer.apple.com/documentation/uikit/uicollectionview/1618099-indexpathsforselecteditems)とかでとれます
|
2
誤字訂正
answer
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
override func viewDidLoad() {
|
35
35
|
super.viewDidLoad()
|
36
36
|
|
37
|
-
// 選択の
|
37
|
+
// 選択の条件を設定する
|
38
38
|
//collectionView.allowsSelection = true
|
39
39
|
collectionView.allowsMultipleSelection = true
|
40
40
|
}
|
@@ -50,7 +50,6 @@
|
|
50
50
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
|
51
51
|
cell.label.text = String(format: "%02d", indexPath.row)
|
52
52
|
// ここで背景を設定するだけ
|
53
|
-
|
54
53
|
cell.selectedBackgroundView = CellBgView(num: indexPath.row)
|
55
54
|
return cell
|
56
55
|
}
|
1
編集
answer
CHANGED
@@ -1,6 +1,93 @@
|
|
1
|
-

|
2
2
|
|
3
3
|
`CellForItemAtn`内で`UICollectionView`の`selectedBackgroundView`に用意したカスタムViewを突っ込んで、
|
4
4
|
UICollectionViewの`allowSelection`/`allowsMultipleSelection`プロパティを設定してあげれば、あとは何も記述しなくともよしなにやってくれます。
|
5
5
|
|
6
|
-
|
6
|
+
私はこの方法が良いと思います。
|
7
|
+
|
8
|
+
```swift
|
9
|
+
import UIKit
|
10
|
+
|
11
|
+
// 背景用のView
|
12
|
+
class CellBgView: UIView {
|
13
|
+
|
14
|
+
init(num: Int) {
|
15
|
+
super.init(frame: CGRect.zero)
|
16
|
+
// 偶数と奇数で色変えてみよう
|
17
|
+
backgroundColor = num % 2 == 0 ? .red : .orange
|
18
|
+
}
|
19
|
+
|
20
|
+
required init(coder aDecoder: NSCoder) {
|
21
|
+
fatalError("init(coder:) has not been implemented")
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
class ViewController: UIViewController{
|
26
|
+
|
27
|
+
var array = [Int](0...30)
|
28
|
+
|
29
|
+
@IBOutlet weak var collectionView: UICollectionView!
|
30
|
+
|
31
|
+
private let sectionInsets = UIEdgeInsets(top: 20.0, left: 10.0, bottom: 20.0, right: 10.0)
|
32
|
+
private let itemsPerRow: CGFloat = 3
|
33
|
+
|
34
|
+
override func viewDidLoad() {
|
35
|
+
super.viewDidLoad()
|
36
|
+
|
37
|
+
// 選択の情景を設定する
|
38
|
+
//collectionView.allowsSelection = true
|
39
|
+
collectionView.allowsMultipleSelection = true
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
extension ViewController: UICollectionViewDataSource {
|
44
|
+
|
45
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
46
|
+
return array.count
|
47
|
+
}
|
48
|
+
|
49
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
50
|
+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
|
51
|
+
cell.label.text = String(format: "%02d", indexPath.row)
|
52
|
+
// ここで背景を設定するだけ
|
53
|
+
|
54
|
+
cell.selectedBackgroundView = CellBgView(num: indexPath.row)
|
55
|
+
return cell
|
56
|
+
}
|
57
|
+
|
58
|
+
}
|
59
|
+
|
60
|
+
class CustomCell: UICollectionViewCell {
|
61
|
+
|
62
|
+
@IBOutlet weak var label: UILabel!
|
63
|
+
|
64
|
+
}
|
65
|
+
|
66
|
+
|
67
|
+
// あとはレイアウト用です
|
68
|
+
|
69
|
+
extension ViewController: UICollectionViewDelegateFlowLayout {
|
70
|
+
|
71
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
72
|
+
|
73
|
+
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
|
74
|
+
let availableWidth = UIScreen.main.bounds.width - paddingSpace
|
75
|
+
let withPerItem = availableWidth / itemsPerRow
|
76
|
+
|
77
|
+
//cellの大きさを返す
|
78
|
+
return CGSize(width: withPerItem, height: withPerItem)
|
79
|
+
}
|
80
|
+
|
81
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
|
82
|
+
|
83
|
+
return sectionInsets
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
88
|
+
|
89
|
+
return sectionInsets.left
|
90
|
+
|
91
|
+
}
|
92
|
+
}
|
93
|
+
```
|