回答編集履歴

1

サンプルコードの追加

2015/08/05 16:05

投稿

Stripe
Stripe

スコア2183

test CHANGED
@@ -1 +1,71 @@
1
1
  UICollectionViewLayoutのサブクラスを作って、Cellのサイズをカスタマイズしてください。
2
+
3
+ ---
4
+
5
+ サンプルコード
6
+
7
+ ```Swift
8
+
9
+ import UIKit
10
+
11
+
12
+
13
+ class CollectionViewLayout: UICollectionViewLayout {
14
+
15
+
16
+
17
+ override func collectionViewContentSize() -> CGSize {
18
+
19
+ let width = self.collectionView!.bounds.width
20
+
21
+ let numOfItems = self.collectionView!.numberOfItemsInSection(0)
22
+
23
+ let lastAttribute = layoutAttributesForItemAtIndexPath(NSIndexPath(forRow: numOfItems-1, inSection: 0))
24
+
25
+ let height = lastAttribute!.frame.maxY + 20
26
+
27
+ return CGSizeMake(width, height)
28
+
29
+ }
30
+
31
+
32
+
33
+ override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
34
+
35
+ let numOfItems = self.collectionView!.numberOfItemsInSection(0)
36
+
37
+ return (0..<numOfItems).map {
38
+
39
+ self.layoutAttributesForItemAtIndexPath(NSIndexPath(forRow: $0, inSection: 0))!
40
+
41
+ }
42
+
43
+ }
44
+
45
+
46
+
47
+ override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
48
+
49
+ let attribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
50
+
51
+ let contentWidth = self.collectionView!.bounds.width
52
+
53
+ let row = CGFloat(indexPath.row)
54
+
55
+ let x:CGFloat = 20.0
56
+
57
+ let y:CGFloat = row * (row * 20.0 + 40.0) + 20.0
58
+
59
+ let width:CGFloat = contentWidth - 40.0
60
+
61
+ let height:CGFloat = row * 40.0 + 40.0
62
+
63
+ attribute.frame = CGRectMake(x, y, width, height)
64
+
65
+ return attribute
66
+
67
+ }
68
+
69
+ }
70
+
71
+ ```