あなたは正しい配列を選択するシンプルなif
/else
またはswitch
例を使用することができます。
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return cubesToWorkWith[row]
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray[row]
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray[row]
} else /* You did not mention what to show for other selections, that would be handled here */ {
return "undefined"
}
}
}
あなたはnumberOfRowsInComponent
に同じをコピーして、配列のcount
を返すことができます。
完全なコード:
class timerScreen: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
// where all the outlets are setup
@IBOutlet weak var pickerViewOutlet: UIPickerView!
// where I try to set up all my variables and constants
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let firstArray = ["cross", "OLL", "Pll", "Corners", "Edges"]
let secondArray = ["OLL", "Pll"]
var lastSelectedCube = 0
// where the picker view is set up.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return cubesToWorkWith.count
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray.count
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray.count
} else /* You did not mention what to show for other selections, that would be handled here */ {
return 0
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return cubesToWorkWith[row]
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray[row]
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray[row]
} else /* You did not mention what to show for other selections, that would be handled here */ {
return "undefined"
}
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
lastSelectedCube = row
pickerView.reloadComponent(1)
}
}
}
として両方の句は非常に似ている場合、あなたは今、余分な方法でそれらをリファクタリングすることができます
...
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let contentArray = content(for: component)
return contentArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let contentArray = content(for: component)
if row >= contentArray.count {
return nil
}
return contentArray[row]
}
func content(for component: Int) -> [String] {
if component == 0 {
return cubesToWorkWith
} else {
if cubesToWorkWith[lastSelectedCube] == "3X3" {
return firstArray
} else if cubesToWorkWith[lastSelectedCube] == "2X2" {
return secondArray
} else /* You did not mention what to show for other selections, that would be handled here */ {
return []
}
}
}
...
Imがエラーを取得 " '[String]'型の戻り式を 'String'型に変換できません。 "内のgetArrayForRow(行)の中に入れ子になっています –
答えは間違っています。コンパイルすべきではありません。文字列が必要な場所にArrayが返されます。 ' - > String!'も意味をなさない。 – shallowThought
私は答えを更新しました。実際にはその戻り値の型はStringであり、Stringの配列を返します。そのタイプの誤字。私はxcodeやツールでこれをコンパイルしていませんでした。あなたに道順を教えてくれるように私の推定された答えは – iProgrammer