From c40ec232cd90a8190ad427b7cb63da4ca7e30279 Mon Sep 17 00:00:00 2001 From: Yoon Lee Date: Thu, 14 Sep 2017 15:05:41 -0700 Subject: [PATCH 1/5] Allocate UITableView at the setup. Insert into superview only if it is not there. --- UIDropDown/UIDropDown.swift | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/UIDropDown/UIDropDown.swift b/UIDropDown/UIDropDown.swift index 7814e38..c0cabd2 100644 --- a/UIDropDown/UIDropDown.swift +++ b/UIDropDown/UIDropDown.swift @@ -160,16 +160,6 @@ public class UIDropDown: UIControl { self.layer.borderWidth = borderWidth self.layer.borderColor = borderColor.cgColor self.addTarget(self, action: #selector(touch), for: .touchUpInside) - } - - @objc fileprivate func touch() { - isSelected = !isSelected - isSelected ? showTable() : hideTable() - } - - fileprivate func showTable() { - - privateTableWillAppear() table = UITableView(frame: CGRect(x: self.frame.minX, y: self.frame.minY, @@ -182,7 +172,21 @@ public class UIDropDown: UIControl { table.layer.borderWidth = optionsBorderWidth table.layer.borderColor = optionsBorderColor.cgColor table.rowHeight = rowHeight ?? table.rowHeight - self.superview?.insertSubview(table, belowSubview: self) + } + + @objc fileprivate func touch() { + isSelected = !isSelected + isSelected ? showTable() : hideTable() + } + + fileprivate func showTable() { + + privateTableWillAppear() + + self.table.isHidden = false + if (self.table.superview) == nil { + self.superview?.insertSubview(table, belowSubview: self) + } switch animationType { case .Default: @@ -271,7 +275,7 @@ public class UIDropDown: UIControl { self.arrow.position = .down }, completion: { (didFinish) -> Void in - self.table.removeFromSuperview() + self.table.isHidden = true self.isSelected = false self.privateTableDidDisappear() }) @@ -292,7 +296,7 @@ public class UIDropDown: UIControl { self.arrow.position = .down }, completion: { (didFinish) -> Void in - self.table.removeFromSuperview() + self.table.isHidden = true self.isSelected = false self.privateTableDidDisappear() }) From e55ff7b98d7221cb7c39258dfb326ae9d0eb04cc Mon Sep 17 00:00:00 2001 From: Yoon Lee Date: Mon, 23 Jul 2018 15:04:39 -0700 Subject: [PATCH 2/5] tableview select action removal for personal usage --- UIDropDown/UIDropDown.swift | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/UIDropDown/UIDropDown.swift b/UIDropDown/UIDropDown.swift index c0cabd2..110e17f 100644 --- a/UIDropDown/UIDropDown.swift +++ b/UIDropDown/UIDropDown.swift @@ -358,19 +358,6 @@ extension UIDropDown: UITableViewDataSource { extension UIDropDown: UITableViewDelegate { public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - - selectedIndex = (indexPath as NSIndexPath).row - - title.alpha = 0.0 - title.text = "\(self.options[(indexPath as NSIndexPath).row])" - - UIView.animate(withDuration: 0.6, - animations: { () -> Void in - self.title.alpha = 1.0 - }) - - tableView.reloadData() - if hideOptionsWhenSelect { hideTable() } From 3ecee47ffc3f9212ea898acd8e6c3aac7e70e00c Mon Sep 17 00:00:00 2001 From: Yoon Lee Date: Mon, 23 Jul 2018 15:33:14 -0700 Subject: [PATCH 3/5] fixed bug --- UIDropDown/UIDropDown.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UIDropDown/UIDropDown.swift b/UIDropDown/UIDropDown.swift index 110e17f..1e091c8 100644 --- a/UIDropDown/UIDropDown.swift +++ b/UIDropDown/UIDropDown.swift @@ -362,7 +362,7 @@ extension UIDropDown: UITableViewDelegate { hideTable() } - privatedidSelect("\(self.options[indexPath.row])", selectedIndex!) + privatedidSelect("\(self.options[indexPath.row])", indexPath/.row!) } } From f14acb9a415fbc8af7060c563be0c380c113decd Mon Sep 17 00:00:00 2001 From: Yoon Lee Date: Mon, 23 Jul 2018 15:36:09 -0700 Subject: [PATCH 4/5] typo fix --- UIDropDown/UIDropDown.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UIDropDown/UIDropDown.swift b/UIDropDown/UIDropDown.swift index 1e091c8..d0846ab 100644 --- a/UIDropDown/UIDropDown.swift +++ b/UIDropDown/UIDropDown.swift @@ -362,7 +362,7 @@ extension UIDropDown: UITableViewDelegate { hideTable() } - privatedidSelect("\(self.options[indexPath.row])", indexPath/.row!) + privatedidSelect("\(self.options[indexPath.row])", indexPath.row!) } } From a25aa4372127cefcb9e0214d65eedf9a6823107d Mon Sep 17 00:00:00 2001 From: Yoon Lee Date: Mon, 23 Jul 2018 15:38:38 -0700 Subject: [PATCH 5/5] removing unwrap usage in Int --- UIDropDown/UIDropDown.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UIDropDown/UIDropDown.swift b/UIDropDown/UIDropDown.swift index d0846ab..fa49c20 100644 --- a/UIDropDown/UIDropDown.swift +++ b/UIDropDown/UIDropDown.swift @@ -362,7 +362,7 @@ extension UIDropDown: UITableViewDelegate { hideTable() } - privatedidSelect("\(self.options[indexPath.row])", indexPath.row!) + privatedidSelect("\(self.options[indexPath.row])", indexPath.row) } }