0

J'ai trois Pickerview comme indiqué dans l'image ci-dessous. Je picklin principal, gauche pickerview, et pickerview à droite.En ce qui concerne Multiple UIPickerview

Le tableau de la sélection principale a un groupe de noms et chaque nom a un groupe d'unités. J'ai fait un tableau dans array afin de leur montrer où il appartient. Mais je ne pouvais pas le faire. Le tableau pour pivkerview principal apparaît à la fois à droite et à gauche des aperçus.

enter image description here

voici mon code:

import UIKit 

classe ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

@IBOutlet weak var mainPicker: UIPickerView! 
@IBOutlet weak var leftPicker: UIPickerView! 
@IBOutlet weak var rightPicker: UIPickerView! 
@IBOutlet weak var textFieldLeft: UITextField! 
@IBOutlet weak var textFielfRight: UITextField! 
@IBOutlet weak var equal: UILabel! 

var mainPickerData = [] 
var leftPickerData = [] 
var rightPickerData = [] 

//yourPicker.backgroundColor = UIColor(patternImage: UIImage(named: "back.jpg")!) 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Connect data: 
    self.mainPicker.delegate = self 
    self.mainPicker.dataSource = self 

    self.leftPicker.delegate = self 
    self.leftPicker.dataSource = self 

    self.rightPicker.delegate = self 
    self.rightPicker.dataSource = self 

    let theWidth = view.frame.size.width 
    let theHeight = view.frame.size.height 

    mainPicker.center = CGPointMake(theWidth/2, theHeight/2 - 182.5) 
    leftPicker.center = CGPointMake(theWidth/2 - 100, theHeight/2) 
    rightPicker.center = CGPointMake(theWidth/2 + 100, theHeight/2) 
    textFieldLeft.center = CGPointMake(theWidth/2 - 90, theHeight/2 + 110) 
    textFielfRight.center = CGPointMake(theWidth/2 + 90, theHeight/2 + 110) 
    equal.center = CGPointMake(theWidth/2, theHeight/2 + 110) 



    let Area = ["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"] 

    let Energy = ["Btus", "Calories", "Ergs", "Foot-Pounds", "Joules", "Kilogram-Calories", "Kilogram-Meters", "Kilowatt-Hours", "Newton-Meters", "Watt-Hours"] 

    let Length = ["Mile", "Yard", "Foot", "Inch", "Kilometer", "Meter", "Centimeter", "Millimeter"] 

    let Power = ["Btus/Minute", "Foot-Pounds/Min", "Foot-Pounds/Sec", "Horsepower", "Kilowatts", "Watts"] 

    let Pressure = ["Pounds/Sqr Ft", "Pounds/Sqr In", "Atmospheres", "Bars", "In of Mercury", "Cm of Mercury", "Kilograms/Sqr Meter", "Pascals"] 

    let Speed = ["Knots", "Miles/Hr", "Miles/Min", "Feet/Min", "Feet/Sec", "Kilometers/Hr", "Kilometer/Min", "Meters/Sec"] 

    let Temperature = ["Celsius C˚", "Fahrenheit", "Kelvin"] 

    let Time = ["Years", "Months", "Weeks", "Days", "Hours", "Minutes", "Seconds", "Millisconds", "Microseconds", " Nanoseconds"] 

    let Volume = ["Cupic Feet","Cubic Meter", "Gallon (Imp)", "Gallon (US)", "Quart (US)", "Pint (US)", "Fluid Oz", "Cup", "Tablespoon", "Teaspoon", "Dram (US)", "Liter"] 

    let Weight = ["Short Ton (US)","Long Ton (UK)", "Pound (U.S)", "Ounce (US)", "Stone", "Metric Ton", "Kilogram", "Gram"] 

    // Pickerview at the center top side of Screen. 
    mainPickerData = ["Area", "Energy", "Length", "Power", "Pressure", "Speed", "Temperature", "Time", "Volume", "Weight"] 

    // Pickerview at the lower left side of Screen. 
    leftPickerData = [["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"], ["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"], ["Mile", "Yard", "Foot", "Inch", "Kilometer", "Meter", "Centimeter", "Millimeter"], ["Btus/Minute", "Foot-Pounds/Min", "Foot-Pounds/Sec", "Horsepower", "Kilowatts", "Watts"], ["Pounds/Sqr Ft", "Pounds/Sqr In", "Atmospheres", "Bars", "In of Mercury", "Cm of Mercury", "Kilograms/Sqr Meter", "Pascals"], ["Knots", "Miles/Hr", "Miles/Min", "Feet/Min", "Feet/Sec", "Kilometers/Hr", "Kilometer/Min", "Meters/Sec"],["Celsius C˚", "Fahrenheit", "Kelvin"], ["Years", "Months", "Weeks", "Days", "Hours", "Minutes", "Seconds", "Millisconds", "Microseconds", " Nanoseconds"], ["Cupic Feet","Cubic Meter", "Gallon (Imp)", "Gallon (US)", "Quart (US)", "Pint (US)", "Fluid Oz", "Cup", "Tablespoon", "Teaspoon", "Dram (US)", "Liter"], ["Short Ton (US)","Long Ton (UK)", "Pound (U.S)", "Ounce (US)", "Stone", "Metric Ton", "Kilogram", "Gram"]] 

    // Pickerview at the lower Right side of Screen. 
    rightPickerData = [["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"], ["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"], ["Mile", "Yard", "Foot", "Inch", "Kilometer", "Meter", "Centimeter", "Millimeter"], ["Btus/Minute", "Foot-Pounds/Min", "Foot-Pounds/Sec", "Horsepower", "Kilowatts", "Watts"], ["Pounds/Sqr Ft", "Pounds/Sqr In", "Atmospheres", "Bars", "In of Mercury", "Cm of Mercury", "Kilograms/Sqr Meter", "Pascals"], ["Knots", "Miles/Hr", "Miles/Min", "Feet/Min", "Feet/Sec", "Kilometers/Hr", "Kilometer/Min", "Meters/Sec"],["Celsius C˚", "Fahrenheit", "Kelvin"], ["Years", "Months", "Weeks", "Days", "Hours", "Minutes", "Seconds", "Millisconds", "Microseconds", " Nanoseconds"], ["Cupic Feet","Cubic Meter", "Gallon (Imp)", "Gallon (US)", "Quart (US)", "Pint (US)", "Fluid Oz", "Cup", "Tablespoon", "Teaspoon", "Dram (US)", "Liter"], ["Short Ton (US)","Long Ton (UK)", "Pound (U.S)", "Ounce (US)", "Stone", "Metric Ton", "Kilogram", "Gram"]] 
} 

// The number of columns of data 
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 1 
} 

// The number of rows of data 
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    if mainPicker.tag == 1 { 

     return mainPickerData.count 

    }else if leftPicker.tag == 2 { 

     return leftPickerData.count 

    }else{ 

     return rightPickerData.count 
    } 
} 

// The data to return for the row and component (column) that's being passed in 
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

    if mainPicker.tag == 1 { 

     return mainPickerData[row] as? String 
    }else if leftPicker.tag == 2 { 

     return leftPickerData[row] as? String 

    }else{ 

     return rightPickerData[row] as? String 
    } 


} 

// Catpure the picker view selection 
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    // This method is triggered whenever the user makes a change to the picker selection. 
    // The parameter named row and component represents what was selected. 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    self.view.endEditing(true) 

} 

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
    let titleData = mainPickerData[row] 
    let myTitle = NSAttributedString(string: titleData as! String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blueColor()]) 
    return myTitle 
} 

}

Répondre

0

Au lieu de prendre un Array, créer un Dictionary pour votre source de données principale comme ceci et l'emploient correctement pour obtenir numberOfRowsInComponent compte et titleForRow. Si vous avez quitté et que le sélecteur de droite est supposé contenir les mêmes données, vous pouvez utiliser une source de données pour cela.

également dans le délégué func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?

ne vérifie pas l'étiquette sur votre instance de sélecteur, si vous avez déjà défini des balises pour vos ramasseurs, puis utilisez l'objet pickerView dans la signature de la méthode pour vérifier tag.Do il comme celui-ci

if pickerView.tag == 1 {...} 
else if (pickerView.tag == 2) {...} 

la raison pour laquelle des données montre même dans tous les Préparateur est que dans votre func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? la condition est toujours vrai. c'est-à-dire mainPicker.tag == 1, si vous avez défini l'étiquette 1 pour mainPicker, par conséquent, elle renvoie les mêmes données pour tous les sélecteurs.

SOLUTION MISE À JOUR

// The number of columns of data 
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 1 
} 

// The number of rows of data 
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    switch (pickerView.tag) { 

    case mainPicker.tag: 

     return mainPickerData.count 

    case leftPicker.tag,rightPicker.tag: 

     let currentSelectedIndex = mainPicker.selectedRowInComponent(0) 
     leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray) 

     return leftRightPickerData.count; 

    default: 
     break; 
    } 
    return 0; 
}//F.E 

// The data to return for the row and component (column) that's being passed in 
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

    switch (pickerView.tag) { 

    case mainPicker.tag: 

     return mainPickerData[row] as? String; 

    case leftPicker.tag,rightPicker.tag: 

     let currentSelectedIndex = mainPicker.selectedRowInComponent(0) 
     leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray) 

     return leftRightPickerData[row] as? String; 

    default: 
     break; 
    } 

    return ""; 

} 

// Catpure the picker view selection 
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    // This method is triggered whenever the user makes a change to the picker selection. 
    // The parameter named row and component represents what was selected. 
    if(pickerView.tag == 1){ 

     let currentSelectedIndex = mainPicker.selectedRowInComponent(0) 
     leftRightPickerData = (dataDict.objectForKey(mainPickerData[currentSelectedIndex] as! String) as! NSArray) 

     leftPicker.reloadAllComponents(); 
     rightPicker.reloadAllComponents(); 
    } 
} 

ci-dessus est la mise en œuvre de votre UIPickerViewdelegate et datasource. Ecrire le code suivant dans votre viewDidLoad()

dataDict = ["Area":["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"] 
     ,"Energy":["Btus", "Calories", "Ergs", "Foot-Pounds", "Joules", "Kilogram-Calories", "Kilogram-Meters", "Kilowatt-Hours", "Newton-Meters", "Watt-Hours"]] 
    mainPickerData = dataDict.allKeys; 
    leftRightPickerData = dataDict.objectForKey(mainPickerData.firstObject as! String) as! NSArray 

Je viens ai ajouté 2 paires clé/valeur pour votre code, vous pouvez ajouter reste des valeurs dans dataDict.

Collez le code suivant dans votre classe avant viewDidLoad()

var dataDict:NSMutableDictionary! 
var mainPickerData:NSArray! 
var leftRightPickerData:NSArray! 
+0

Oui vous bien en ce qui concerne la gauche et à droite pickerview, ils supposent ont les mêmes données. Je suis désolé d'avoir des questions pour vous ... 1- Comment puis-je partager les données et déléguer entre les aperçus gauche et droit. Dois-je supprimer le délégué et sourse de la mainpickerview. 2- let leftPickerData = dataDict.objectForKey ("Énergie"); Qu'en est-il des autres groupes? Comment puis-je les ajouter dans la même variable?et dois-je faire la même chose pour la droite ?? 3- –

+0

pour votre dernier commentaire concernant l'étiquette, que dois-je faire. Actuellement, rien n'apparaît dans aucun d'entre eux. –

+0

Vérifiez ma réponse mise à jour, j'espère qu'elle va résoudre votre problème et sur la ligne de changement dans votre sélecteur principal la vue gauche et droite sera mise à jour en conséquence – Muneeba