Skip to content

Commit 8e89a67

Browse files
author
Suric zhang
committed
fix crash when set tableview seperatorStyle
1 parent 6f2f7a5 commit 8e89a67

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

DropdownMenu/DropUpMenu.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ open class DropUpMenu: UIView {
8686

8787
clipsToBounds = true
8888
setupGestureView()
89+
initTableView()
8990
}
9091

9192
fileprivate func setupGestureView() {
@@ -101,35 +102,38 @@ open class DropUpMenu: UIView {
101102
gestureView.addGestureRecognizer(tapGestureRecognizer)
102103
}
103104

104-
fileprivate func setupBottomSeperatorView() {
105-
let seperatorView = UIView()
106-
seperatorView.backgroundColor = tableViewSeperatorColor
107-
addSubview(seperatorView)
108-
seperatorView.translatesAutoresizingMaskIntoConstraints = false
109-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)])
110-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
111-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
112-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
105+
fileprivate func initTableView() {
106+
tableView = UITableView(frame: CGRect.zero, style: .grouped)
107+
tableView?.delegate = self
108+
tableView?.dataSource = self
109+
addSubview(tableView)
113110
}
114111

115-
fileprivate func setupTableView() {
112+
fileprivate func layoutTableView() {
116113
tableViewHeight = CGFloat(items.count) * rowHeight
117114
let maxHeight = UIScreen.main.bounds.height - bottomOffsetY
118115
if tableViewHeight > maxHeight {
119116
tableViewHeight = maxHeight
120117
}
121118

122-
tableView = UITableView(frame: CGRect.zero, style: .grouped)
123-
tableView?.delegate = self
124-
tableView?.dataSource = self
125-
addSubview(tableView)
126119
tableView.translatesAutoresizingMaskIntoConstraints = false
127120
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant:0)])
128121
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: tableViewHeight)])
129122
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
130123
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
131124
}
132125

126+
fileprivate func setupBottomSeperatorView() {
127+
let seperatorView = UIView()
128+
seperatorView.backgroundColor = tableViewSeperatorColor
129+
addSubview(seperatorView)
130+
seperatorView.translatesAutoresizingMaskIntoConstraints = false
131+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)])
132+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
133+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
134+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
135+
}
136+
133137
fileprivate func setupBottomCoverView(on view: UIView) {
134138
barCoverView = UIView()
135139
barCoverView.backgroundColor = UIColor.clear
@@ -150,7 +154,7 @@ open class DropUpMenu: UIView {
150154
}
151155
isShow = true
152156

153-
setupTableView()
157+
layoutTableView()
154158
setupBottomSeperatorView()
155159

156160
if let rootView = UIApplication.shared.keyWindow {

DropdownMenu/DropdownMenu.swift

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ open class DropdownMenu: UIView {
101101

102102
clipsToBounds = true
103103
setupGestureView()
104+
initTableView()
104105

105106
NotificationCenter.default.addObserver(self, selector: #selector(self.updateForOrientationChange(_:)), name: NSNotification.Name.UIApplicationWillChangeStatusBarOrientation, object: nil)
106107
}
@@ -115,6 +116,7 @@ open class DropdownMenu: UIView {
115116

116117
clipsToBounds = true
117118
setupGestureView()
119+
initTableView()
118120

119121
NotificationCenter.default.addObserver(self, selector: #selector(self.updateForOrientationChange(_:)), name: NSNotification.Name.UIApplicationWillChangeStatusBarOrientation, object: nil)
120122
}
@@ -159,36 +161,40 @@ open class DropdownMenu: UIView {
159161
gestureView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideMenu)))
160162
}
161163

162-
fileprivate func setupTopSeperatorView() {
163-
let seperatorView = UIView()
164-
seperatorView.backgroundColor = tableViewSeperatorColor
165-
addSubview(seperatorView)
166-
seperatorView.translatesAutoresizingMaskIntoConstraints = false
167-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)])
168-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
169-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
170-
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
164+
fileprivate func initTableView() {
165+
tableView = UITableView(frame: CGRect.zero, style: .grouped)
166+
tableView.separatorStyle = separatorStyle
167+
tableView?.delegate = self
168+
tableView?.dataSource = self
169+
addSubview(tableView)
171170
}
172171

173-
fileprivate func setupTableView() {
172+
fileprivate func layoutTableView() {
173+
tableView.translatesAutoresizingMaskIntoConstraints = false
174+
174175
tableViewHeight = tableviewHeight()
175176
let maxHeight = navigationController.view.frame.height - topLayoutConstraintConstant - defaultBottonMargin
176177
if tableViewHeight > maxHeight {
177178
tableViewHeight = maxHeight
178179
}
179180

180-
tableView = UITableView(frame: CGRect.zero, style: .grouped)
181-
tableView.separatorStyle = separatorStyle
182-
tableView?.delegate = self
183-
tableView?.dataSource = self
184-
addSubview(tableView)
185-
tableView.translatesAutoresizingMaskIntoConstraints = false
186181
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant:0)])
187182
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: tableViewHeight)])
188183
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
189184
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
190185
}
191186

187+
fileprivate func setupTopSeperatorView() {
188+
let seperatorView = UIView()
189+
seperatorView.backgroundColor = tableViewSeperatorColor
190+
addSubview(seperatorView)
191+
seperatorView.translatesAutoresizingMaskIntoConstraints = false
192+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)])
193+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
194+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
195+
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
196+
}
197+
192198
fileprivate func setupNavigationBarCoverView(on view: UIView) {
193199
barCoverView = UIView()
194200
barCoverView?.backgroundColor = UIColor.clear
@@ -223,7 +229,7 @@ open class DropdownMenu: UIView {
223229

224230
isShow = true
225231

226-
setupTableView()
232+
layoutTableView()
227233
setupTopSeperatorView()
228234

229235
if let rootView = UIApplication.shared.keyWindow {

DropdownMenuDemo/DropdownMenuDemo/ViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class ViewController: UIViewController {
5151
menuView = DropdownMenu(navigationController: navigationController!, items: [item1, item2, item3, item4], selectedRow: selectedRow)
5252
}
5353
menuView?.topOffsetY = CGFloat(offSetSlider.value)
54+
menuView?.separatorStyle = .none
5455
menuView?.delegate = self
55-
menuView?.rowHeight = 70
56+
menuView?.rowHeight = 50
5657
menuView?.showMenu()
5758
}
5859

0 commit comments

Comments
 (0)