//
// ViewController.swift
// PMBUI
//
// Created by ScofieldRoger on 3/3/15.
// Copyright (c) 2015 ScofieldRoger. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var pickerView1: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView1.delegate = self
pickerView1.dataSource = self
}
var names = ["computers","phones","mp3"]
var products = ["computers":["iMac","MacBook Pro","MacBook Air"],"phones":["iPhone 5","iPhone 5s","iPhone 6 Plus"],"mp3":["iPod touch","iPod Nano"]]
var selector : String = "computers"
//pickerView1 DataSource Part
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 2
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if (component == 0) {
return names.count
}
else {return products[selector]!.count}
}
//响应事件,根据点击载入项目
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
if (component == 0) {
selector = names[row]
pickerView1.reloadComponent(1)
//每点一个项目,重载一次products
pickerView.selectRow(0, inComponent: 1, animated: true)
//将选项重新定位到第一个
return names[row]
}
else {return products[selector]![row]}
// else { return "?"}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}