gpt4 book ai didi

iphone - 如何在 Swift 的 UIPickerView 中划分三个部分

转载 作者:搜寻专家 更新时间:2023-11-01 05:48:13 27 4
gpt4 key购买 nike

我有 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)")
}
}

最佳答案

你做得对。通常组件就像 TableView 中的部分,行就是行。 didSelect 委托(delegate)方法传递所选的组件和行,因此您可以从模型中找到实际选择的特定数据。这是一个小样本,

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")

}
}

}

如果您希望自定义选择器 View ,您可以使用以下委托(delegate)。

– pickerView:attributedTitleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:

关于iphone - 如何在 Swift 的 UIPickerView 中划分三个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25524769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com