title: Drop down list menu programmatically in Swift Xcode
body: <p>I have made a drop down <code>gender</code> which is working fine however I have another drop down on same <code>viewcontroller</code> which shows drop down but when I select the item it gives error <code>fatal error: unexpectedly found nil while unwrapping an Optional value</code> I have tried out but confused. help me to solve the <code>bloodList</code> dropdown.</p>
  
  <pre><code>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) -&gt; Int {
      return 1
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return dropDownList.count
  
  }
  
  func tableViewBlood(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return bloodList.count
  }
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      return 50
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = genderTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
      cell.textLabel!.text = dropDownList[indexPath.row]
      return cell
  }
  
  func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; 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)")
  }
  }
  </code></pre>