@@ -79,9 +79,9 @@ class CollectionViewWaterfallLayout: UICollectionViewLayout {
79
79
}
80
80
81
81
//MARK: Private Properties
82
- private var delegate : CollectionViewWaterfallLayoutDelegate {
82
+ private weak var delegate : CollectionViewWaterfallLayoutDelegate ? {
83
83
get {
84
- return collectionView. delegate as CollectionViewWaterfallLayoutDelegate
84
+ return collectionView. delegate as? CollectionViewWaterfallLayoutDelegate
85
85
}
86
86
}
87
87
private var columnHeights = [ Float] ( )
@@ -102,7 +102,7 @@ class CollectionViewWaterfallLayout: UICollectionViewLayout {
102
102
return ;
103
103
}
104
104
105
- assert ( delegate. conformsToProtocol ( CollectionViewWaterfallLayoutDelegate) , " UICollectionView's delegate should conform to WaterfallLayoutDelegate protocol " )
105
+ assert ( delegate? . conformsToProtocol ( CollectionViewWaterfallLayoutDelegate) , " UICollectionView's delegate should conform to WaterfallLayoutDelegate protocol " )
106
106
assert ( columnCount > 0 , " WaterfallFlowLayout's columnCount should be greater than 0 " )
107
107
108
108
// Initialize variables
@@ -126,15 +126,15 @@ class CollectionViewWaterfallLayout: UICollectionViewLayout {
126
126
* 1. Get section-specific metrics (minimumInteritemSpacing, sectionInset)
127
127
*/
128
128
var minimumInteritemSpacing : Float
129
- if let height = delegate. collectionView ? ( collectionView, layout: self , minimumInteritemSpacingForSection: section) {
129
+ if let height = delegate? . collectionView ? ( collectionView, layout: self , minimumInteritemSpacingForSection: section) {
130
130
minimumInteritemSpacing = height
131
131
}
132
132
else {
133
133
minimumInteritemSpacing = self . minimumInteritemSpacing
134
134
}
135
135
136
136
var sectionInset : UIEdgeInsets
137
- if let inset = delegate. collectionView ? ( collectionView, layout: self , insetForSection: section) {
137
+ if let inset = delegate? . collectionView ? ( collectionView, layout: self , insetForSection: section) {
138
138
sectionInset = inset
139
139
}
140
140
else {
@@ -148,15 +148,15 @@ class CollectionViewWaterfallLayout: UICollectionViewLayout {
148
148
* 2. Section header
149
149
*/
150
150
var headerHeight : Float
151
- if let height = delegate. collectionView ? ( collectionView, layout: self , heightForHeaderInSection: section) {
151
+ if let height = delegate? . collectionView ? ( collectionView, layout: self , heightForHeaderInSection: section) {
152
152
headerHeight = height
153
153
}
154
154
else {
155
155
headerHeight = self . headerHeight
156
156
}
157
157
158
158
var headerInset : UIEdgeInsets
159
- if let inset = delegate. collectionView ? ( collectionView, layout: self , insetForHeaderInSection: section) {
159
+ if let inset = delegate? . collectionView ? ( collectionView, layout: self , insetForHeaderInSection: section) {
160
160
headerInset = inset
161
161
}
162
162
else {
@@ -194,10 +194,10 @@ class CollectionViewWaterfallLayout: UICollectionViewLayout {
194
194
195
195
let xOffset = Float ( sectionInset. left) + Float( itemWidth + minimumColumnSpacing) * Float( columnIndex)
196
196
let yOffset = columnHeights [ columnIndex]
197
- let itemSize = delegate. collectionView ( collectionView, layout: self , sizeForItemAtIndexPath: indexPath)
197
+ let itemSize = delegate? . collectionView ( collectionView, layout: self , sizeForItemAtIndexPath: indexPath)
198
198
var itemHeight : Float = 0.0
199
- if itemSize. height > 0 && itemSize. width > 0 {
200
- itemHeight = Float ( itemSize. height) * itemWidth / Float( itemSize. width)
199
+ if itemSize? . height > 0 && itemSize? . width > 0 {
200
+ itemHeight = Float ( itemSize! . height) * itemWidth / Float( itemSize! . width)
201
201
}
202
202
203
203
attributes = UICollectionViewLayoutAttributes ( forCellWithIndexPath: indexPath)
@@ -216,15 +216,15 @@ class CollectionViewWaterfallLayout: UICollectionViewLayout {
216
216
var columnIndex = longestColumnIndex ( )
217
217
top = columnHeights [ columnIndex] - minimumInteritemSpacing + Float( sectionInset. bottom)
218
218
219
- if let height = delegate. collectionView ? ( collectionView, layout: self , heightForFooterInSection: section) {
219
+ if let height = delegate? . collectionView ? ( collectionView, layout: self , heightForFooterInSection: section) {
220
220
footerHeight = height
221
221
}
222
222
else {
223
223
footerHeight = self . footerHeight
224
224
}
225
225
226
226
var footerInset : UIEdgeInsets
227
- if let inset = delegate. collectionView ? ( collectionView, layout: self , insetForFooterInSection: section) {
227
+ if let inset = delegate? . collectionView ? ( collectionView, layout: self , insetForFooterInSection: section) {
228
228
footerInset = inset
229
229
}
230
230
else {
0 commit comments