Swiftで遊ぼう! - 326 - My Picker Project : DependentPickerViewでメソッド準拠
Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide
質問 : Swiftで遊ぼう! - 252 - FaceViewプロジェクト始動 - Swiftで遊ぼう! on Hatena
PickerViewを動かすために必ずメソッドの実装をしなければいけません。
何度も繰り返しているから復習ですね。
何度やっても覚えられないけど...
Data Sourceメソッドで必須は2つ。
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 2 }
コンポーネントの数は2つなので、2を返す。
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if component == stateComponent { return states!.count } else { return zips!.count } }
そして次にそのコンポーネントにいくつ項目が含まれているのかコンポーネント別に定義します。
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { if component == stateComponent { return states[row] } else { return zips[row] } }
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if component == stateComponent { let selectedState = states![row] zips = stateZips[selectedState] dependentPicker.reloadComponent(zipComponent) dependentPicker.selectRow(0, inComponent: zipComponent, animated: true) } }
これが依存Pickerの要でしす。Stateコンポーネントに操作が加わると、それをベースにしてZipコンポーネントを一番最初の項目で再表示させます。
これでなんとかできました。
今日はここまで。