IOS swift UIPickerview问题
Posted
技术标签:
【中文标题】IOS swift UIPickerview问题【英文标题】:IOS swift UIPickerview issue 【发布时间】:2015-09-17 16:59:09 【问题描述】:如何在我的视图控制器中添加其他选择器视图
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var UnitTypeLabel: UILabel!
@IBOutlet weak var UnitTypeSelectBTN: UIButton!
@IBOutlet weak var UnitTypePicker: UIPickerView!
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
let UnitforLengthData = ["m","km","cm","mm","yd","ft","in"]
let UnitforWeightData = ["g","kg","m.t","l.t","sh.t","lb"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return 1
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return UnitDataArray.count
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return UnitDataArray[row]
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
return UnitTypeLabel.text = UnitDataArray[row]
override func viewDidLoad()
super.viewDidLoad()
//UnitTypePicker.hidden = true
UnitTypePicker.delegate = self
UnitTypePicker.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
【参考方案1】:应该是这样的,你必须实现 pickerView viewForRow 方法来实现:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var pickerView: UIPickerView!
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
override func viewDidLoad()
super.viewDidLoad()
// pickerView delegate n dataSource
pickerView.dataSource = self
pickerView.delegate = self
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return 1
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return UnitDataArray.count
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
return UnitDataArray[row]
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = UnitDataArray[row]
if let pickerViewFont = UIFont(name: "HelveticaNeue-Thin", size: 30)
pickerLabel.font = pickerViewFont
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
return 40.0
【讨论】:
如果我想添加两个 pcikerview ,我可以只添加第二个数组,但是如何。 如果你想要两个pickerView,你必须在storyboard中创建第二个pickerView,然后将它拖到IBOutlet,然后像这样改变你的代理:func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int if pickerView == youFirstPickerViewIBOutlet return UnitDataArray.count else return ArrayForTheSecondPickerView.count
你应该对titleForRow和viewForRow做同样的事情方法。
我可以使用 IBAction 来切换选择器视图还是需要使用 IBOutlet : UIButton
您在“切换选择器视图”下是什么意思?您想在窗口中有两个选择器视图还是只有一个并通过按下按钮来切换其数据?
对不起,只有一个,按按钮切换数据【参考方案2】:
如果您想要更多数量的pickerView,那么您必须像这样设置其中的组件数量:
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int
return 2 //number of pickerViews
通过改变数量,你可以在同一个viewController中设置pickerView的数量。
但是,如果您想在任何操作后仅更改一个pickerView 的数据集,那么您可以看到How do I reload/refresh the UIPickerView (with new data array) based on button press?
【讨论】:
但是如果return 2会在一个pickerView中显示2行,对吧?以上是关于IOS swift UIPickerview问题的主要内容,如果未能解决你的问题,请参考以下文章
ios swift 2如何用json数据填充uipickerview [重复]
如何在 iOS Swift 3 中滚动到 UIPickerView 中的特定行
Swift 中的第一个 iOS 应用程序 - UIPickerView 中的问号
如何解决问题 UIPickerView 致命错误:索引超出范围 [IOS Swift 5.1 Xcode 11.2]