【问题标题】:UIPickerView multi components in swiftswift中的UIPickerView多组件
【发布时间】:2017-07-08 02:11:03
【问题描述】:

我找到了这个答案How do I setup a second component with a UIPickerView,它很有帮助,但我想做更多。我想让第二个组件的数据依赖于第一个组件。这是我的代码

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”,我希望在第二个组件中显示 secondArray。你会怎么做? 提前感谢您的帮助!!!

【问题讨论】:

    标签: swift uipickerview


    【解决方案1】:

    您可以使用简单的if/else 或switch 案例来选择正确的数组:

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {        
        if component == 0 {
            return cubesToWorkWith[row]
        } else {
            if cubesToWorkWith[lastSelectedCube] == "3X3" {
                return firstArray[row]
            } else if cubesToWorkWith[lastSelectedCube] == "2X2" {
                return secondArray[row]
            } else /* You did not mention what to show for other selections, that would be handled here */ {
                return "undefined"
            }
        }
    }
    

    您可以将其复制到numberOfRowsInComponent 并返回数组的count。


    完整代码:

    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"]
        var lastSelectedCube = 0
        // where the picker view is set up.
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 2
        }
    
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    
            if component == 0 {
                return cubesToWorkWith.count
            } else {
                if cubesToWorkWith[lastSelectedCube] == "3X3" {
                    return firstArray.count
                } else if cubesToWorkWith[lastSelectedCube] == "2X2" {
                    return secondArray.count
                } else /* You did not mention what to show for other selections, that would be handled here */ {
                    return 0
                }
            }
        }
    
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    
            if component == 0 {
                return cubesToWorkWith[row]
            } else {
                if cubesToWorkWith[lastSelectedCube] == "3X3" {
                    return firstArray[row]
                } else if cubesToWorkWith[lastSelectedCube] == "2X2" {
                    return secondArray[row]
                } else /* You did not mention what to show for other selections, that would be handled here */ {
                    return "undefined"
                }
            }
        }
    
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if component == 0 {
                lastSelectedCube = row
                pickerView.reloadComponent(1)
            }
        }
    }
    

    由于两个 if 子句看起来非常相似,您现在可以用一个额外的方法重构它们:

    ...
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {        
        let contentArray = content(for: component)
    
        return contentArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let contentArray = content(for: component)
    
        if row >= contentArray.count {
            return nil
        }
    
        return contentArray[row]
    }
    
    func content(for component: Int) -> [String] {
        if component == 0 {
            return cubesToWorkWith
        } else {
            if cubesToWorkWith[lastSelectedCube] == "3X3" {
                return firstArray
            } else if cubesToWorkWith[lastSelectedCube] == "2X2" {
                return secondArray
            } else /* You did not mention what to show for other selections, that would be handled here */ {
                return []
            }
        }
    }
    ...
    

    【讨论】:

      【解决方案2】:

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

      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 []
          } 
      }
      

      【讨论】:

      • 我收到一个错误“无法将“[String]”类型的返回表达式转换为“String”类型的返回值? " 在嵌套在 titleForRow 中的 getArrayForRow(row) 中
      • 答案是错误的。它甚至不应该编译。在期望 String 的地方返回一个 Array 。 -> String! 也没有任何意义。
      • 我更新了答案。实际上它的返回类型是字符串,我正在返回字符串数组。它是一种错字。我没有在 xcode 或工具上编译它,它只是我估计的答案,给你们指导
      • 如果我们发现答案错误或奇怪,我们也有责任编辑答案!我们正在帮助社区。 Downvote 不是任何用户发布的建议的解决方案!
      • 还是错了。 getArrayForRow(row)[row] 大部分时间都会崩溃。
      猜你喜欢
      • 2019-10-27
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多