2017-03-01 64 views
2

pickerview我想在迅速 這是單行選擇多行選擇爲SWIFT 3.0

`進口的UIKit來選擇pickerview多行

類的ViewController:UIViewController中,UIPickerViewDelegate,UIPickerViewDataSource {

@IBOutlet weak var titlelabel: UILabel! 
@IBOutlet weak var pickerview: UIPickerView! 
var cars = ["benz","audi","lombagini","rangerrover","bently"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    pickerview.delegate = self 
    pickerview.dataSource = self 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func numberOfComponents(in pickerView: UIPickerView) -> Int { 
return 1 
} 
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    return cars.count 

} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { 

    return cars[row] 

} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    titlelabel.text = cars[row] 
} 

}`

沒有任何一個有任何想法來選擇picke多行r查看

in objective-c AlPickerview但它不起作用。

+0

那麼你是否得到了解決方案? –

回答

0
func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 2 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    if component == 0{ 
     return bloodgroup.count 
    } 
    else if component == 1 { 
     return districk.count 
    } 
    return 1 
} 
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    if component == 0{ 
     return bloodgroup[row] 
    } 
    else if component == 1 { 
     return districk[row] 
    } 
    return "" 

} 
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    if component == 0{ 
     print(bloodgroup[row]) 
    } 
    else if component == 1 { 
     print(districk[row]) 
    } 

} 
+0

添加一些解釋。 – Billa