88
99import Foundation
1010
11- public protocol _StackScrollViewCellType {
11+ public protocol BetaStackScrollViewCellType : class {
1212
1313}
1414
15+ extension BetaStackScrollViewCellType where Self: UIView {
16+
17+ public var stackScrollView : BetaStackScrollView {
18+
19+ var superview : UIView ? = self
20+
21+ while !( superview is BetaStackScrollView ) {
22+ superview = superview? . superview
23+ }
24+
25+ precondition ( superview is BetaStackScrollView , " Must be added StackScrollView " )
26+ return superview as! BetaStackScrollView
27+ }
28+
29+ public func updateLayout( animated: Bool ) {
30+ stackScrollView. updateLayout ( animated: animated)
31+ }
32+
33+ public func set( isHidden: Bool , animated: Bool ) {
34+
35+ stackScrollView. setHidden ( isHidden: isHidden, view: self , animated: animated)
36+ }
37+
38+ public func scrollToSelf( animated: Bool ) {
39+
40+ stackScrollView. scroll ( to: self , animated: animated)
41+ }
42+ }
43+
1544open class BetaStackScrollView : UIView , UITableViewDataSource , UITableViewDelegate {
1645
1746 public override init ( frame: CGRect ) {
@@ -55,6 +84,12 @@ open class BetaStackScrollView: UIView, UITableViewDataSource, UITableViewDelega
5584
5685 // TODO: Improve performance, animated
5786 tableView. reloadData ( )
87+ if animated {
88+ UIView . animate ( withDuration: 0.3 , animations: {
89+ self . tableView. beginUpdates ( )
90+ self . tableView. endUpdates ( )
91+ } )
92+ }
5893 }
5994
6095 open func append( views: [ UIView ] , animated: Bool ) {
@@ -64,6 +99,7 @@ open class BetaStackScrollView: UIView, UITableViewDataSource, UITableViewDelega
6499 cells += _cells
65100 // TODO: Improve performance, animated
66101 tableView. reloadData ( )
102+ updateLayout ( animated: animated)
67103 }
68104
69105 open func remove( view: UIView , animated: Bool ) {
@@ -74,12 +110,37 @@ open class BetaStackScrollView: UIView, UITableViewDataSource, UITableViewDelega
74110
75111 // TODO: Improve performance, animated
76112 tableView. reloadData ( )
113+ updateLayout ( animated: animated)
114+ }
115+
116+ open func setHidden( isHidden: Bool , view: UIView , animated: Bool ) {
117+
118+ view. isHidden = isHidden
119+ updateLayout ( animated: animated)
120+ }
121+
122+ open func scroll( to view: UIView , animated: Bool ) {
123+
124+ let targetRect = view. convert ( view. bounds, to: self )
125+ tableView. scrollRectToVisible ( targetRect, animated: true )
77126 }
78127
79128 public var cells : [ UITableViewCell ] = [ ]
80129
81130 private let tableView = UITableView ( frame: . zero, style: . plain)
82131
132+ public func updateLayout( animated: Bool ) {
133+ if animated {
134+ UIView . animate ( withDuration: 0.3 , animations: {
135+ self . tableView. beginUpdates ( )
136+ self . tableView. endUpdates ( )
137+ } )
138+ } else {
139+ tableView. beginUpdates ( )
140+ tableView. endUpdates ( )
141+ }
142+ }
143+
83144 private func createCell( view: UIView ) -> UITableViewCell {
84145 let cell = UITableViewCell ( frame: . zero)
85146 cell. separatorInset = . zero
@@ -115,9 +176,17 @@ open class BetaStackScrollView: UIView, UITableViewDataSource, UITableViewDelega
115176
116177 public func tableView( _ tableView: UITableView , heightForRowAt indexPath: IndexPath ) -> CGFloat {
117178
179+ // iOS8 Bugs
180+
118181 let cell = cells [ indexPath. row]
119182
120183 let contentView = cell. contentView
184+ contentView. invalidateIntrinsicContentSize ( )
185+
186+ guard contentView. subviews. first? . isHidden == false else {
187+ return 0
188+ }
189+
121190 let width = NSLayoutConstraint ( item: contentView, attribute: . width, relatedBy: . equal, toItem: nil , attribute: . width, multiplier: 1 , constant: tableView. bounds. width)
122191
123192 NSLayoutConstraint . activate ( [ width] )
0 commit comments