我有一个快速的选择器视图,包含两个组件。除了当我想用selectrow在第二个组件中设置行时,它才有效。这是用户重新加载应用程序时我们已经设置了值。第一个组件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个值。它完全忽略了这一点。
有人告诉我我做错了什么吗?谢谢。
答案 0 :(得分:0)
所以,问一个问题,然后找出答案..
答案是将selectrow移动到ViewDidAppear中。
我在这里找到了解决方案: UIPickerView selectRow doesn't works
由于