gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:34:16 24 4
gpt4 key购买 nike

我做了一个下拉 gender ,它工作正常但是我在同一个 viewcontroller 上有另一个下拉,它显示下拉但是当我选择项目时它给出错误fatal error: expectedly 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)")
}
}

最佳答案

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

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

对其余的委托(delegate)方法也进行类似的更改。

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

关于ios - 在 Swift Xcode 中以编程方式下拉列表菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43387312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com