在 Swift Xcode 中以编程方式下拉列表菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43387312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:54:36  来源:igfitidea点击:

Drop down list menu programmatically in Swift Xcode

iosswiftxcodedrop-down-menu

提问by Xcodian Solangi

I have made a drop down genderwhich is working fine however I have another drop down on same viewcontrollerwhich shows drop down but when I select the item it gives error fatal error: unexpectedly found nil while unwrapping an Optional valueI have tried out but confused. help me to solve the bloodListdropdown.

我做了一个gender工作正常的下拉菜单,但是我有另一个下拉菜单,viewcontroller它显示了下拉菜单,但是当我选择该项目时,它给出了错误,fatal error: unexpectedly found nil while unwrapping an Optional value我尝试过但很困惑。帮我解决bloodList下拉列表。

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

采纳答案by Xcodian Solangi

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 requireany irritating third party libraryor awkward pods

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

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

回答by Sneha

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

您不能更改 tableView 的默认委托方法,请在委托方法中提供如下条件:

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

回答by karthikeyan

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 delegatemethods of UITableview, you should override it, you cannot change it.

它们都在UITableview 的构建委托方法中,你应该覆盖它,你不能改变它。

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

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

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 {