Step #1 : Add Pod and install
pod 'DropDown'
Step #2: Adding a drop-down in UIButton Tap
import UIKit
import DropDown
class ViewController: UIViewController {
let dropDown = DropDown()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapChooseItems(_ sender: UIButton) {
dropDown.dataSource = ["option 1", "option 2", "option 3"]
dropDown.anchorView = sender
dropDown.bottomOffset = CGPoint(x: 0, y: sender.frame.size.height)
dropDown.show()
dropDown.selectionAction = { [weak self] (index: Int, item: String) in
guard let _ = self else { return }
sender.setTitle(item, for: .normal)
}
}
}
Step #3: Adding a drop-down inside UITableView
import UIKit
import DropDown
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let dropDown = DropDown()
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Result: \(indexPath.row+1): "
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
dropDown.dataSource = ["option 1", "option 2", "option 3"]
dropDown.anchorView = cell
dropDown.bottomOffset = CGPoint(x: 0, y: cell.frame.size.height)
dropDown.backgroundColor = .gray
dropDown.show()
dropDown.selectionAction = { [weak self] (index: Int, item: String) in
guard let _ = self else { return }
cell.textLabel?.text = "Result: \(indexPath.row+1): \(item)"
}
}
}
}