我有三个Pickerview,如下图所示。我主要的挑选视图,左挑选视图和正确的挑选视图。
主pickerview数组有一组名称,每个名称都有一组单位。我已经在数组内部进行了数组,以便向它们显示它所属的位置。但我不能这样做。主要的kekerview数组在pickerviews的thr righ和left中都显示出来。
这是我的代码:
import UIKit
类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
}
}
答案 0 :(得分:0)
不是采用Array
,而是为您的主数据源创建Dictionary
,并正确使用它来获取numberOfRowsInComponent
计数和titleForRow
。
let dataDict:NSMutableDictionary = ["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"]]
let mainPickerData = dataDict.allKeys;
let leftPickerData = dataDict.objectForKey("Energy"); //here you can pas the string return from pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) delegate for main picker
print(mainPickerData);
print(leftPickerData);
如果你左右选择器应该包含相同的数据,你可以使用一个数据源。
同样在代理人func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
不检查选择器实例上的标记,如果您已经为选择器设置了标记,则使用方法签名中的pickerView
对象来检查标记。这样做
if pickerView.tag == 1 {...}
else if (pickerView.tag == 2) {...}
数据在所有选择器中显示相同的原因是func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
条件总是如此。即mainPicker.tag == 1
,如果你为mainPicker设置了标签1,那么它返回所有选择器的数据相同。
更新的解决方案
// 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();
}
}
以上是UIPickerView
delegate
和datasource
的实施方案。
在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
我刚刚为您的代码添加了2个键/值对,您可以在dataDict
中添加其余值。
在viewDidLoad()
var dataDict:NSMutableDictionary!
var mainPickerData:NSArray!
var leftRightPickerData:NSArray!