代码之家  ›  专栏  ›  技术社区  ›  Wings

如何在swift的pickerview中返回两个单独的数组

  •  1
  • Wings  · 技术社区  · 8 年前

    我创造了一个 PickerView 在一个 SegmentControl ,我有两个单独的数组FirstArray有 StringData ,第二个数组具有 NSNumber 数据。为了返回 字符串数据 同一行的数字。

    For Example: First Array has : Ark,silk,temp etc, Second Array has 23,45,56 etc.

    如果我单独返回第一个数组 选择视图 它正在返回字符串值,但我需要同时显示两个数组。我已经完成了在选择器视图中显示一个数组的工作,但不知道如何返回两个数组。 代码:

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    
       if (pickerView == EventsPickerView) {
    
       // case EventsPickerView:
            return EventArray.count
    
       }else {
    
        switch FabricSegmentControl.selectedSegmentIndex {
    
        case 0:
    
            return globalConstants.ClassificationName.count
              //  return globalConstants.ClassificationName.count + globalConstants.ClassificationCount.count
    
        case 1:
    
            return globalConstants.ClassificationNameSecond.count
    
    
    
    
    
        default:
            return 1
        }
    }
      //  return 1
    }
    
     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    
     if  (pickerView == EventsPickerView){
    
       // case EventsPickerView:
            return EventArray[row]
    
     }else {
    
        switch FabricSegmentControl.selectedSegmentIndex {
    
        case 0:
    
            return globalConstants.ClassificationName[row] as? String
    
    
    
    
        case 1:
    
            return globalConstants.ClassificationNameSecond[row] as? String
    
    
    
        default:
            return "error occured"
        }
    
    }
    
       // return "error occured"
    
    
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
        if (pickerView == EventsPickerView){
    
            eventTextField.text = EventArray[row]
        }else {
    
            switch FabricSegmentControl.selectedSegmentIndex {
    
            case 0:
    
    
    
                    TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String
    
    
    
    
            case 1:
    
                  TypeOfSilkTextField.text = globalConstants.ClassificationNameSecond[row] as? String
    
    
            default:
                break
            }
        }
    

    这些是我的数组名

    First Array :globalConstants.ClassificationName.count

    SecondArray name :globalConstants.ClassificationCount.count

    2 回复  |  直到 8 年前
        1
  •  2
  •   Shehata Gamal    8 年前

    因为两个数组相等,所以返回其中一个

    return globalConstants.ClassificationName.count
    

    / /

    titleForRow

     return globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )
    

    / /

    didSelectRow

    TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String ?? ""  + ( globalConstants.ClassificationCount[row] as? String ?? "" )
    

    但最好创建一个结构来将它们作为一个单元

    / /

    怎么说才有意义呢

     static let ClassificationName = ["value","value2"]
    

    它将被推断为字符串数组 [String] ---到 return ClassificationName[row]

     static let ClassificationCount = [25,26]
    

    它将被推断为整数数组 [Int] ---到 return "\(ClassificationCount[row])"

    或是

     static let ClassificationCount = ["25","26"]
    

    它将被推断为字符串数组 [字符串] ---到 return ClassificationCount[row]

        2
  •  3
  •   shivi_shub    8 年前

    您正在以字符串形式重新输入nsnumber。这就是零值返回的原因。 尝试将nsnumber转换为字符串。