Drop down list menu programmatically in Swift Xcode

Drop down list menu programmatically in Swift Xcode

drop down list in ios swift 4
drop down menu swift 4 github
swift navigation bar drop down menu
how to create drop down list in xcode
textfield with dropdown swift
uitableviewcell drop down swift
pod 'dropdown swift 4
dropdown in swift 4 github

I have made a drop down gender which is working fine however I have another drop down on same viewcontroller which shows drop down but when I select the item it gives error fatal error: unexpectedly found nil while unwrapping an Optional value I have tried out but confused. help me to solve the bloodList dropdown.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var genderButton: UIButton!
@IBAction func genderButton(_ sender: Any) {
    self.PressDrop()
    view.addSubview(genderTable)
}
@IBOutlet weak var genderTable: UITableView!

var flag = 1
var dropDownList = [String]()
@IBOutlet weak var bloodButton: UIButton!
var bloodList = [String]()
@IBAction func bloodButton(_ sender: Any) {
    self.PressBlood()
    view.addSubview(bloodTable)
}
@IBOutlet weak var bloodTable: UITableView!

    override func viewDidLoad() {
    super.viewDidLoad()

    dropDownList = ["Male", "Female", "Other"]
    genderTable.delegate = self
    genderTable.dataSource = self
    genderTable.isHidden = true
    view.addSubview(genderTable) 
    genderTable.layer.cornerRadius = 10

    bloodList = ["A+", "A-", "AB+", "AB-"] 
    bloodTable.delegate = self
    bloodTable.dataSource = self
    bloodTable.isHidden = true
    view.addSubview(bloodTable)
    bloodTable.layer.cornerRadius = 10  
}

func PressDrop() {
    if flag == 0 {
        UIView.setAnimationDuration(0.5)
        self.genderTable.isHidden = true
        self.flag = 1
    }
    else{
        UIView.setAnimationDuration(0.5)
        self.genderTable.isHidden = false
        self.flag = 0
    }
}

func PressBlood() {
    if flag == 0 {
        UIView.setAnimationDuration(0.5)
        self.bloodTable.isHidden = true
        self.flag = 1
    }
    else{
        UIView.setAnimationDuration(0.5)
        self.bloodTable.isHidden = false
        self.flag = 0
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dropDownList.count

}

func tableViewBlood(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return bloodList.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel!.text = dropDownList[indexPath.row]
    return cell
}

func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = bloodTable.dequeueReusableCell(withIdentifier: "bloodCell", for: indexPath)
    cell.textLabel!.text = bloodList[indexPath.row]
    return cell
}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let   selectedData = dropDownList[indexPath.row]
    genderButton.setTitle(selectedData, for: .normal)
    self.genderTable.isHidden = true
    self.flag = 
    let indexPath = genderTable.indexPathForSelectedRow
    let currentCell = genderTable.cellForRow(at: indexPath!)! as UITableViewCell
    let finalresult = currentCell.textLabel!.text!
    print("\(finalresult)")
}

private func tableViewTwo(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let   selectedBlood = bloodList[indexPath.row]
    bloodButton.setTitle(selectedBlood, for: .normal)
    self.bloodTable.isHidden = true
    self.flag = 1
    let indexPathTwo = bloodTable.indexPathForSelectedRow
    let currentCellBlood = bloodTable.cellForRow(at: indexPathTwo!)! as UITableViewCell
    let finalresultBlood = currentCellBlood.textLabel!.text!
    print("\(finalresultBlood)")
}
}

You cannot change default delegate methods of your tableView, give conditions in the delegate methods like below :

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      if tableView == genderTable {
          return dropDownList.count
      }
      return bloodList.count
    }

Also make similar changes in the remaining delegate methods.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == genderTable {
        let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel!.text = dropDownList[indexPath.row]
        return cell
} else {
        let cell = bloodTable.dequeueReusableCell(withIdentifier: "bloodCell", for: indexPath)
        cell.textLabel!.text = bloodList[indexPath.row]
        return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      if tableView == genderTable { 
        let   selectedData = dropDownList[indexPath.row]
        genderButton.setTitle(selectedData, for: .normal)
        self.genderTable.isHidden = true 
        let currentCell = genderTable.cellForRow(at: indexPath)! as UITableViewCell
        let finalresult = currentCell.textLabel!.text!
        print("\(finalresult)")
     } else {
        let   selectedBlood = bloodList[indexPath.row]
        bloodButton.setTitle(selectedBlood, for: .normal)
        self.bloodTable.isHidden = true
        let currentCellBlood = bloodTable.cellForRow(at: indexPath)! as UITableViewCell
        let finalresultBlood = currentCellBlood.textLabel!.text!
        print("\(finalresultBlood)")
   }
}

Let's make a Drop Down Menu! (Swift 4 in Xcode 9), DevMountain (Sponsor) - https://goo.gl/kgkx3r Today we dive deep into making our very own Duration: 31:14 Posted: Nov 3, 2017 Hello All, In this video, we are going to talk about how we can create the Drop Down Menu. This is programmatically drop down which change the position according to the button on which this is


Don't create like this

func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell (This will consider as new method to your current class)

They are all in build delegate methods of UITableview, you should override it, you cannot change it.

Instead of creating method, you try with Bool variable in respective button action.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

If isDrop == true{
    return dropDownList.count
}else{
return bloodList.count
}

}

Like change the condition

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

Swift 4.2: How to Create DropDown list dynamically (Xcode 10.1 , Hello All, In this video, we are going to talk about how we can create the Drop Down Menu Duration: 22:49 Posted: Jul 28, 2019 Learn how to create a dropdown menu like a professional by using a UITableView and an enumeration. I also show you how to link an action to each dropdown menu item. All views are created


I waited for an answer but did not find :( that's ok . I thought a number of times and then come to this code yahoooo!!! then i believed i am really a programmer. Actually i did not defined the condition for each table view so i was in nightmare. This code is pure programatically and does not require any irritating third party library or awkward pods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == tableViewB {
    return dropDownList.count
    }
    else {
    return genderL.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == tableViewB { 
    let cell = tableViewB.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel!.text = dropDownList[indexPath.row]
    return cell     
    }  
    else {
        let cell = genderT.dequeueReusableCell(withIdentifier: "gender", for: indexPath)
        cell.textLabel!.text = genderL[indexPath.row]
        return cell  
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == tableViewB {
    let   selectedData = dropDownList[indexPath.row]
    buttonB.setTitle(selectedData, for: .normal)
    self.tableViewB.isHidden = true
    self.flag = 1
    let indexPath = tableViewB.indexPathForSelectedRow
    let currentCell = tableViewB.cellForRow(at: indexPath!)! as UITableViewCell
    let finalresult = currentCell.textLabel!.text!
    print("\(finalresult)")
    }
    else {
        let   selectedDataG = genderL[indexPath.row]
        genderB.setTitle(selectedDataG, for: .normal)
        self.genderT.isHidden = true
        self.flag = 1
        let indexPath = genderT.indexPathForSelectedRow
        let currentCell = genderT.cellForRow(at: indexPath!)! as UITableViewCell
        let finalresult = currentCell.textLabel!.text!
        print("\(finalresult)")
    }  
}

How To Make A Drop Down Menu The Right Way | Swift 4, Learn how to create a dropdown menu like a professional by using a menu item. All views Duration: 23:17 Posted: Dec 22, 2018 Hello Everyone, In this tutorial, we are going to learn about how we can create a simple drop-down menu or drop-down list which contains some list so we can select any item in that list.


iOS Swift DropDown Menu - Prathamesh Salvi, Open Xcode, add new Swift file and put this code. This MakeDropDown class will make the DropDown Menu. You will need an object of MakeDropDown class to  Learn how to implement a drop-down menu in Swift 4 with UIStackViews. StackViews handle laying out our drop down items and save us from typing out a lot of U


Custom UI Master Class: Dropdown Menu with TextField, Dropdown menus should be employed sparingly and reserved for situations when you want your user to select from a fairly limited range of options. Visually the  DevMountain (Sponsor) - https://goo.gl/kgkx3r Today we dive deep into making our very own drop down menus from scratch, using Swift 4 in Xcode! Enjoy! :D Pro


Creating a Dropdown Field in Swift for iOS, When an option is selected, the text field will be populated with the selected option, as seen below. dropdown field ios image003. Method 1. The  I need help trying to create a dropdown menu or list in xcode 6 in the Swift language. I don't even know where to start, can anyone provide me with a starting point or a tutorial I could follow? Thanks. All your help is very much appreciated.