Skip to content

Commit 54dbc26

Browse files
committed
Test performance
1 parent 2942a1d commit 54dbc26

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

StackScrollView/StackScrollView.swift

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
3232
static let width = "me.muukii.StackScrollView.width"
3333
}
3434

35-
private(set) open var views: [UIView] = []
35+
open var views: [UIView] {
36+
return source.map { $0.0 }
37+
}
38+
39+
private var source: [(UIView, String)] = []
3640

3741
public convenience init() {
3842
self.init(frame: .zero)
@@ -69,22 +73,26 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
6973

7074
open func append(view: UIView) {
7175

72-
views.append(view)
76+
let key = UUID().uuidString
77+
source.append((view, key))
78+
79+
register(Cell.self, forCellWithReuseIdentifier: key)
7380

7481
reloadData()
7582
}
7683

7784
open func append(views: [UIView]) {
7885

79-
self.views += views
86+
views.forEach {
87+
append(view: $0)
88+
}
8089

81-
reloadData()
8290
}
8391

8492
open func remove(view: UIView, animated: Bool) {
8593

86-
if let index = views.index(of: view) {
87-
views.remove(at: index)
94+
if let index = source.map({ $0.0 }).index(of: view) {
95+
source.remove(at: index)
8896
view.removeFromSuperview()
8997
}
9098
}
@@ -96,7 +104,7 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
96104
}
97105

98106
open func scroll(to view: UIView, at position: UICollectionViewScrollPosition, animated: Bool) {
99-
if let index = views.index(of: view) {
107+
if let index = source.map({ $0.0 }).index(of: view) {
100108
scrollToItem(at: IndexPath(item: index, section: 0), at: position, animated: animated)
101109
}
102110
}
@@ -110,14 +118,14 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
110118
}
111119

112120
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
113-
return views.count
121+
return source.count
114122
}
115123

116124
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
117125

118-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
126+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: source[indexPath.item].1, for: indexPath)
119127

120-
let view = views[indexPath.item]
128+
let view = source[indexPath.item].0
121129

122130
if view.superview == cell.contentView {
123131
return cell
@@ -129,7 +137,6 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
129137
}
130138

131139
view.translatesAutoresizingMaskIntoConstraints = false
132-
// cell.contentView.translatesAutoresizingMaskIntoConstraints = false
133140
cell.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
134141

135142
cell.contentView.addSubview(view)
@@ -156,7 +163,7 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
156163

157164
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
158165

159-
let view = views[indexPath.item]
166+
let view = source[indexPath.item].0
160167

161168
let width: NSLayoutConstraint = {
162169

0 commit comments

Comments
 (0)