我的示例代码为UIPickerView
,其中一个部分为整数值。如何使用不同的值制作3个部分 - 整数,字符串等?我如何处理3个部分的didSelectRow
方法?
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate{
@IBOutlet weak var tF1: UITextField!
var progPicker=UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
progPicker.frame=CGRectMake(50, 50, 50, 162)
progPicker.delegate=self
progPicker.tag = 101
progPicker.selectRow(180, inComponent: 0, animated: true)
self.view.addSubview(progPicker)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 250
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String
{
return "\(row)"
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
println("Selected row: \(row)")
}
}
答案 0 :(得分:10)
你正确地做到了。通常,组件类似于表视图中的部分,行是行。 didSelect委托方法传递所选的组件和行,因此您可以从模型中找到实际选择的特定数据。这是一个小样本,
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView(frame: CGRectMake(0, 0, 320, 300))
view.addSubview(pickerView)
pickerView.delegate = self
pickerView.dataSource = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 3
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return 10
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
if component == 0{
return "Int: \(row)"
}else if component == 1{
return "String: \(row)"
}
return "Other: \(row)"
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
if component == 0{
println("Int component: \(row) selected")
}else if component == 1{
println("String component: \(row) selected")
}else{
println("Other component: \(row) selected")
}
}
}
如果您想自定义选择器视图,可以使用以下代理。
– pickerView:attributedTitleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView: