快速的 UIPickerView 多组件 [英] UIPickerView multi components in swift

查看:18
本文介绍了快速的 UIPickerView 多组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个答案 我如何设置一个带有 UIPickerView 的第二个组件,它很有帮助,但我想做更多.我想让第二个组件的数据依赖于第一个.这是我的代码

I found this answer How do I setup a second component with a UIPickerView and it was helpful but I want to do more. I want to have the data of the second component dependent on the first. here is my code

class timerScreen: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
// where all the outlets are setup
@IBOutlet weak var pickerViewOutlet: UIPickerView!
// where I try to set up all my variables and constants
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let firstArray = ["cross", "OLL", "Pll", "Corners", "Edges"]
let SecondArray = ["OLL" "Pll"]

// where the picker view is set up.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return cubesToWorkWith.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return cubesToWorkWith[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    cubeSelected = Int16(row)
}


}

如果当前在第一个组件中选择了3X3",我希望 firstArray 显示在第二个组件中,如果选择了2X2",则第二个数组显示.你会怎么做?提前感谢您的任何帮助!!!

I want the firstArray to be displayed in the second component if the "3X3" is currently selected in the first component and the secondArray if the "2X2" is selected. How would you do that? thanks for any help in advance!!!

推荐答案

尝试这样做.我不知道你到底想要什么.但您似乎想在第一个组件的行选择上重新加载组件.如果这是要求,那么此代码将起作用.

Try do like this. I dont know what you exactly want. But it seems you want to reload the components on row selection of first component. If this is the requirement then this code will work.

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    //row = [repeatPickerView selectedRowInComponent:0];
    var row = pickerView.selectedRowInComponent(0)
    println("this is the pickerView\(row)")

    if component == 0 {
        return cubesToWorkWith.count
    }
    else {
        return firstArray.count
    }
}

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

    if component == 0 {
        return cubesToWorkWith[row]
    } else {
        return getArrayForRow(row)[row]
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     if component == 0 {
          pickerView.reloadComponent(1)
     }
}

func getArrayForRow(row: Int) -> [String] {
    switch row { // check if 2*2 0r 3*3 
    case 0:
        return firstArray
    case 1:
        return secondArray
    // and so on...
    default:
        return []
    } 
}

这篇关于快速的 UIPickerView 多组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆