Ios Swift Xcode中的下拉列表菜单

Ios Swift Xcode中的下拉列表菜单,ios,swift,xcode,drop-down-menu,Ios,Swift,Xcode,Drop Down Menu,我做了一个下拉菜单gender,效果很好,但是我在同一个viewcontroller上有另一个下拉菜单,显示下拉菜单,但当我选择该项时,它会给出错误致命错误:在展开可选值时意外发现nil我尝试过,但感到困惑。请帮助我解决血液列表下拉列表 import UIKit class ViewController: UIViewController { @IBOutlet weak var genderButton: UIButton! @IBAction func genderButton(_ se

我做了一个下拉菜单
gender
,效果很好,但是我在同一个
viewcontroller
上有另一个下拉菜单,显示下拉菜单,但当我选择该项时,它会给出错误
致命错误:在展开可选值时意外发现nil
我尝试过,但感到困惑。请帮助我解决
血液列表
下拉列表

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)")
}
}
不要这样创造

func tableViewTwo(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell (This will consider as new method to your current class)
它们都在UITableview的builddelegate方法中,您应该覆盖它,不能更改它

您可以尝试在相应的按钮操作中使用Bool变量,而不是创建方法

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

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

}
比如改变条件

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

无法更改tableView的默认委托方法,请在委托方法中指定以下条件:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      if tableView == genderTable {
          return dropDownList.count
      }
      return bloodList.count
    }
在其余的委托方法中也进行类似的更改

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)")
   }
}

我等待回答,但没有找到:(没关系。我想了很多次,然后想到了这段代码Yahooo!!!然后我相信我真的是一个程序员。实际上我没有为每个表视图定义条件,所以我陷入了噩梦。这段代码纯粹是程序性的,不需要任何恼人的第三方或笨拙的豆荚

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)")
    }  
}

let indexPath=genderTable.indexPathForSelectedRow
给出错误
期望表达式
选择单元格时需要什么?我希望在控制台中打印值要打印的值是什么?文本标签是什么?让我们来设置断点并尝试精确指出可空性错误的位置。只需返回所有内容即可你的代码没有帮助。