我使用了UIPickerView并且它给了我错误(无法转换类型' [String]'的返回表达式返回类型'字符串?' / UIPickerView )。这是我的代码。
// where the picker view is set up.
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let threeByThreeArray = ["OLL", "PLL"]
@IBOutlet weak var pickerViewOutlet: UIPickerView!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let row = pickerView.selectedRow(inComponent: 0)
print("this is the pickerView\(row)")
switch row {
case 0:
return threeByThreeArray.count
default:
return cubesToWorkWith.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return threeByThreeArray[row]
default:
return getArrayForRow(row: row)
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
cubeSelected = Int16(row)
}
func getArrayForRow(row: Int) -> [String] {
switch row {
case 0:
return threeByThreeArray
default:
return cubesToWorkWith
}
}
}
}
我在titleForRow里面的switch case中得到错误"返回getArrayForRow(row:row)" 谢谢你提前帮助!!!!
答案 0 :(得分:1)
所以你错过了一些东西 首先,您需要将委托广告数据源设置为sekf 并且在titleForRow中:你需要返回一个字符串,这样你的 func getArrayForRow(row:Int) - > [串] 需要像 func getArrayForRow(row:Int) - >串
这是我的提议:
类ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { //设置选择器视图的位置。 let cubesToWorkWith = [“3X3”,“2X2”,“4X4”,“5X5”,“6X6”,“7X7”,“Skewb”,“Square-One”] 让threeByThreeArray = [“OLL”,“PLL”]
@IBOutlet weak var pickerViewOutlet: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.pickerViewOutlet.dataSource = self
self.pickerViewOutlet.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let row = pickerView.selectedRow(inComponent: 0)
print("this is the pickerView\(row)")
switch row {
case 0:
return threeByThreeArray.count
default:
return cubesToWorkWith.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return threeByThreeArray[row] as String
default:
return getArrayForRow(row: row) as String
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// cubeSelected = Int16(row)
}
func getArrayForRow(row: Int) -> String {
switch row {
case 0:
return threeByThreeArray[row]
default:
return cubesToWorkWith[row]
}
}
}