【发布时间】:2017-05-04 08:14:25
【问题描述】:
我正在尝试使我的选择器视图的第一行无法选择,但它不起作用。我尝试了以下选项,但它们都给出了一些错误,或者都适用于以前的 Swift 版本。
- Disable particular row value in UIPickerView
- (iOS) How to make first value in UIPickerView unselectable?
我的代码:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 1){
return list1.count
} else {
return list2.count
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 1){
return "\(list1[row])"
} else {
return "\(list2[row])"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
frequency = list1[row]
} else {
period = list2[row]
}
}
我的数组:
list1 = ["Select", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]
list2 = ["Select", "days", "weeks"]
我想让两个选择器视图中的“选择”都无法选择。此外,如果用户不更改选择器视图选项(尝试继续选择“选择”,即使它是不可选择的),我也想发送一个错误。
继续按钮代码:
@IBAction func addActivity(_ sender: Any) {
if (input.text != "" && pickerString != "Select") {
nameActivity = input.text!
list.append(nameActivity)
input.text = ""
} else if (input.text == "") {
let alertController = UIAlertController(title: "Error", message:
"Please give the activity a name!", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
} else if pickerString == "Select" {
let alertController = UIAlertController(title: "Error", message:
"Select something", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
}
}
【问题讨论】:
-
你不能像你提供的链接那样做吗? didSelectRow 方法中的 row == 0 ?这有什么问题?
-
If pickerView.selectedrowincomponent != 0 { //随心所欲 }
-
@GrzegorzKrukowski 然后我应该将
if (row == 0) { [pickerView selectRow:row+1 inComponent:component animated:YES]; }放在已经定义的“didSelectRow”中吗?因为这给了我一些错误,所以它想放置几个逗号,最后它说Expected expression in container literal。
标签: swift uipickerview