请原谅我的无知。我正在从头开始学习。
我试图根据视图顶部的pickerView的选择填充多个pickerView实例。我希望其余pickerview中显示的选择取决于在第一个pickerview中选择的项目。
我总共有6个pickerViews。第一个进行一般选择,我想将其余部分作为基础。
Pickerview1具有4个组件。如果用户选择组件4,则所有剩余的pickerViews都会显示选择。如果用户选择组件2或3,我希望pickerView 4和5显示N / A,而其他显示选择。如果用户选择组件1,我希望pickerViews 3-5显示N / A,而1-2显示选项。
我假设要执行此操作,我将需要pickerView1中所选组件的索引值来为func pickerView做出if else语句(_ pickerView:UIPickerView,numberOfRowsInComponent等。
我在文档中找到了selectedRow(inComponent :),但是找不到在我的情况下如何实现它的有效示例。
到目前为止,我的代码附带了一些代码片段: 导入UIKit
FirstViewController类:UIViewController,UIPickerViewDelegate, UIPickerViewDataSource { func numberOfComponents(在pickerView中:UIPickerView)-> Int { 返回1 }
var capacities = ["6","9","12","15","18","24"]
var notUsed = "N/A"
var oduList = ["MXZ-2C20", "MXZ-3C24", "MXZ-3C30", "MXZ-5C42"]
var result:Float = 0.0
var one4num:Float = 1.0
var factor:Float = 1.08
var per5ft:Float = 5
var factory:Float = 98
var selectedRow = 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
selectedRow = pickerView.selectedRow(inComponent: index)
if pickerView == oduPicker {
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}
else if pickerView == idu2PickerView {
return capacities.count
}
else if pickerView == idu3PickerView {
return capacities.count
}
else if pickerView == idu4PickerView {
return capacities.count
}
else {
return capacities.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == oduPicker {
return oduList[row]
}
else if pickerView == idu1PickerView {
return capacities[row]
}
else if pickerView == idu2PickerView {
return capacities[row]
}
else {
return capacities[row]
}
}
@IBOutlet weak var oduPicker: UIPickerView!
@IBOutlet weak var idu1PickerView: UIPickerView!
@IBOutlet weak var idu2PickerView: UIPickerView!
@IBOutlet weak var idu3PickerView: UIPickerView!
@IBOutlet weak var idu4PickerView: UIPickerView!
@IBOutlet weak var idu5PickerView: UIPickerView!
@IBOutlet weak var one4Label: UILabel!
@IBOutlet weak var one4Slider: UISlider!
@IBOutlet weak var output: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.oduPicker.delegate = self
self.oduPicker.dataSource = self
self.idu1PickerView.delegate = self
self.idu1PickerView.dataSource = self
self.idu2PickerView.delegate = self
self.idu2PickerView.dataSource = self
self.idu3PickerView.delegate = self
self.idu3PickerView.dataSource = self
}
@IBAction func one4Slider(_ sender: Any) {
one4Label.text = "\(one4Slider.value)"
}
@IBAction func three8Slider(_ sender: Any) {
three8Label.text = "\(three8Slider.value)"
}
@IBAction func calculateButton(_ sender: Any) {
result = factor*(one4num-factory)/per5ft
output.text = "\(result)"
}
}
如果我以这种方式实施建议:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
var capacities = ["6","9","12","15","18","24"]
var notUsed = ["N/A"]
var oduList = ["MXZ-2C20", "MXZ-3C24", "MXZ-3C30", "MXZ-5C42"]
var result:Float = 0.0
var one4num:Float = 1.0
var factor:Float = 1.08
var per5ft:Float = 5
var factory:Float = 98
var rowSelected: Int?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == oduPicker{
self.rowSelected = row
// make anything
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == oduPicker {
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}
else if pickerView == idu2PickerView {
return capacities.count
}
else if pickerView == idu3PickerView {
if rowSelected! < 2 {
return 1
}
else {
return capacities.count
}
}
else if pickerView == idu4PickerView {
if rowSelected! < 3 {
return 1
}
else {
return capacities.count
}
}
else {
if rowSelected! < 4 {
return 1
}
else {
return capacities.count
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == oduPicker {
return oduList[row]
}
else if pickerView == idu1PickerView {
return capacities[row]
}
else if pickerView == idu2PickerView {
return capacities[row]
}
else if pickerView == idu3PickerView {
if rowSelected! < 2 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu4PickerView {
if rowSelected! < 3 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu5PickerView {
if rowSelected! < 4 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else {
return capacities[row]
}
}
@IBOutlet weak var oduPicker: UIPickerView!
@IBOutlet weak var idu1PickerView: UIPickerView!
@IBOutlet weak var idu2PickerView: UIPickerView!
@IBOutlet weak var idu3PickerView: UIPickerView!
@IBOutlet weak var idu4PickerView: UIPickerView!
@IBOutlet weak var idu5PickerView: UIPickerView!
@IBOutlet weak var one4Label: UILabel!
@IBOutlet weak var one4Slider: UISlider!
@IBOutlet weak var output: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.oduPicker.delegate = self
self.oduPicker.dataSource = self
self.idu1PickerView.delegate = self
self.idu1PickerView.dataSource = self
self.idu2PickerView.delegate = self
self.idu2PickerView.dataSource = self
self.idu3PickerView.delegate = self
self.idu3PickerView.dataSource = self
self.idu4PickerView.delegate = self
self.idu4PickerView.dataSource = self
self.idu5PickerView.delegate = self
self.idu5PickerView.dataSource = self
}
@IBAction func one4Slider(_ sender: Any) {
one4Label.text = "\(one4Slider.value)"
}
@IBAction func calculateButton(_ sender: Any) {
result = factor*(one4num-factory)/per5ft
output.text = "\(result)"
}
}
代码破坏了“ FirstViewController”并似乎使“ numberOfRows”和“ titleForRows”语句以及下面的一系列错误无效。
如果我在这两个功能之后实现:
else {
return capacities[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == oduPicker{
self.rowSelected = row
// make anything
}
@IBOutlet weak var oduPicker: UIPickerView!
@IBOutlet weak var idu1PickerView: UIPickerView!
@IBOutlet weak var idu2PickerView: UIPickerView!
@IBOutlet weak var idu3PickerView: UIPickerView!
@IBOutlet weak var idu4PickerView: UIPickerView!
@IBOutlet weak var idu5PickerView: UIPickerView!
它破坏了所有的pickerView变量,例如oduPicker等。
如果我尝试将语句带入第一个函数,则如下所示:
var rowSelected: Int?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, numberOfRowsInComponent component: Int) -> Int {
if pickerView == oduPicker {
self.rowSelected = row
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}...
它再次中断“ FirstViewController”,并显示错误“类型'FirstViewController'不符合协议'UIPickerViewDataSource'”
我也被迫加上“!”在“ rowSelected”之后进行包装/展开。我不确定这是什么意思还是目前重要。
更新:
如果我按照塞缪尔的建议实施这样的话:
import UIKit
FirstViewController类:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { func numberOfComponents(在pickerView中:UIPickerView)-> Int { 返回1 }
var capacities = ["6","9","12","15","18","24"]
var notUsed = ["N/A"]
var oduList = ["MXZ-2C20", "MXZ-3C24", "MXZ-3C30", "MXZ-5C42"]
var result:Float = 0.0
var one4num:Float = 1.0
var factor:Float = 1.08
var per5ft:Float = 5
var factory:Float = 98
var selectedRow = 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
selectedRow = pickerView.selectedRow(inComponent: index)
if pickerView == oduPicker {
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}
else if pickerView == idu2PickerView {
return capacities.count
}
else if pickerView == idu3PickerView {
if selectedRow < 2 {
return 1
}
else {
return capacities.count
}
}
else if pickerView == idu4PickerView {
if selectedRow < 3 {
return 1
}
else {
return capacities.count
}
}
else {
if selectedRow < 4 {
return 1
}
else {
return capacities.count
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == oduPicker {
return oduList[row]
}
else if pickerView == idu1PickerView {
return capacities[row]
}
else if pickerView == idu2PickerView {
return capacities[row]
}
else if pickerView == idu3PickerView {
if selectedRow < 2 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu4PickerView {
if selectedRow < 3 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu5PickerView {
if selectedRow < 4 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else {
return capacities[row]
}
}
@IBOutlet weak var oduPicker: UIPickerView!
@IBOutlet weak var idu1PickerView: UIPickerView!
@IBOutlet weak var idu2PickerView: UIPickerView!
@IBOutlet weak var idu3PickerView: UIPickerView!
@IBOutlet weak var idu4PickerView: UIPickerView!
@IBOutlet weak var idu5PickerView: UIPickerView!
@IBOutlet weak var one4Label: UILabel!
@IBOutlet weak var one4Slider: UISlider!
@IBOutlet weak var output: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.oduPicker.delegate = self
self.oduPicker.dataSource = self
self.idu1PickerView.delegate = self
self.idu1PickerView.dataSource = self
self.idu2PickerView.delegate = self
self.idu2PickerView.dataSource = self
self.idu3PickerView.delegate = self
self.idu3PickerView.dataSource = self
self.idu4PickerView.delegate = self
self.idu4PickerView.dataSource = self
self.idu5PickerView.delegate = self
self.idu5PickerView.dataSource = self
}
@IBAction func one4Slider(_ sender: Any) {
one4Label.text = "\(one4Slider.value)"
}
@IBAction func calculateButton(_ sender: Any) {
result = factor*(one4num-factory)/per5ft
output.text = "\(result)"
}
}
行后出现错误:
selectedRow = pickerView.selectedRow(inComponent: index)
“无法将类型'(Any)-> Int'的值转换为预期的参数类型'Int'”
答案 0 :(得分:0)
您可以使用委托的方法。以下方法可以为您提供帮助。 row
参数特定于用户选择的行。您可以尝试:
var rowSelected : Int?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == idu1PickerView {
self.rowSelected = row
// make anything
}
...
}
如果只有一个组件,则可以使用您编写的方法:
var selectedRow = pickerView.selectedRow(inComponent: 0)