gpt4 book ai didi

swift uipickerview 第二个组件 selectrow 不工作

转载 作者:行者123 更新时间:2023-11-28 08:56:42 25 4
gpt4 key购买 nike

我有一个带有两个组件的 swift pickerview。除了当我想用 selectrow 在第二个组件中设置行时,它可以工作。这是用户重新加载应用程序并且我们已经设置了值的时候。第一个组件选择行有效。这是我的代码:

    override func viewDidLoad() {
super.viewDidLoad()

print("In viewDidLoad")

detailArmy.loadarmy(detailArmy, armymember: detailItem)

myGeneralPicker.dataSource = self
myGeneralPicker.delegate = self
// Set General Picker View to current value
let general = generalTypeInfo()
picker1Options = general.generalType()
print("Detail Item general: \(detailArmy.general)")
if detailArmy.general == "" {
myGeneralPicker.selectRow(0, inComponent: 0, animated: false)
picker2Options = general.generalInfo("None")
} else {
myGeneralPicker.selectRow(picker1Options.indexOf(detailArmy.general)!, inComponent: 0, animated: false)
picker2Options = general.generalInfo(detailArmy.general)
}
print("Detail Item general Type: \(detailArmy.generaltype) picker2Options: \(picker2Options)")
if detailArmy.generaltype == "" {
myGeneralPicker.selectRow(0, inComponent: 1, animated: false)
} else {
//myGeneralPicker.selectRow(picker2Options.indexOf(detailArmy.generaltype)!, inComponent: 1, animated: false)
myGeneralPicker.selectRow(4, inComponent: 1, animated: false)
}
}
//MARK: Delegates and data sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
//println("In numberOfComponenetsInPickerView pickerView: \(pickerView)")
return 2
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return picker1Options.count
} else {
return picker2Options.count
}
}

//Mark: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//var dicerow = dice[row]
print("titleForRow row: \(row) component: \(component)")
if component == 0 {
return String(picker1Options[row])
} else {
return String(picker2Options[row])
}
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
generalpickinfo = picker1Options[row]
print("pickerView general selected: \(generalpickinfo)")
let general = generalTypeInfo()
picker2Options = general.generalInfo(picker1Options[row])
pickerView.reloadAllComponents()
myGeneralPicker.selectRow(0, inComponent: 1, animated: false)
} else {
generalpicktype = picker2Options[row]
print("pickerView general type selected: \(generalpicktype)")
}
}

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
print("viewForRow row: \(row) component: \(component)")
let pickGeneralLabel = UILabel()
pickGeneralLabel.textAlignment = .Center
if component == 0 {
pickGeneralLabel.text = String(picker1Options[row])
} else {
pickGeneralLabel.text = String(picker2Options[row])
}
return pickGeneralLabel
}

class generalTypeInfo {
func generalType() -> [String] {
return ["None","C-in-C","Subordinate","Ally"]
}

func generalInfo(type: String) -> [String] {
if type == "None" {
return ["None"]
} else {
return ["None","Brilliant","Inert"]
}
}

func generalTypeIndex(type: String) -> Int {
let generalTypeDict = ["None": 0, "C-in-C": 1, "Subordinate": 2, "Ally": 3]
return generalTypeDict[type]!
}

func generalInfoIndex(type: String) -> Int {
let generalInfoDict = ["None": 0, "Brilliant": 1, "Inert": 2]
return generalInfoDict[type]!
}
}

在我的代码中,我特意在这一行中输入了 4 的值:

myGeneralPicker.selectRow(4, inComponent: 1, animated: false)

上面的行有我想要运行的正确代码,但我把它放进去是因为它应该使应用程序崩溃,因为没有第 4 个值。它完全忽略了这一点。

有人告诉我我做错了什么吗?谢谢。

最佳答案

所以,问一个问题,然后找出答案..

答案是将选择行移动到 ViewDidAppear 中。

我在这里找到了解决方案: UIPickerView selectRow doesn't works

谢谢

关于swift uipickerview 第二个组件 selectrow 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868766/

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