如何在Swift中为UIPickerView按字母顺序排列数组

时间:2015-05-17 21:48:25

标签: arrays swift uitextfield uipickerview

所以我有一个由字符串组成的数组,当用户按下我的文本字段时,它会显示使用该数组的PickerView。

我希望PickerView按字母顺序显示数组的内容。但我不知道如何做到这一点,并且无法在网上找到如何在Swift中做到这一点。有人可以帮忙吗?

提前致谢!

我的代码:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var questionLabel: UILabel!

@IBOutlet var buttonLabel: UIButton!

@IBOutlet var myBackgroundView: UIImageView!

@IBOutlet var questionTextField: UITextField!

let questions = ["Where are you going?", "Which city?", "When do you go?"]

var currentQuestionIndex = 0

let placeholder = ["Country", "City", "Date"]

var currentPlaceholderIndex = 0

@IBAction func nextButton(sender: AnyObject) {

// Initial setup on button press
questionTextField.hidden = false

barImage.hidden = false

// Reset text field to have no text
questionTextField.text = ""

// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex <= questions.count && currentPlaceholderIndex <= placeholder.count {

    questionLabel.text = questions[currentQuestionIndex]
    questionTextField.placeholder = placeholder[currentPlaceholderIndex]

    currentQuestionIndex++

    currentPlaceholderIndex++

    buttonLabel.setTitle("Next", forState: UIControlState.Normal)


    // Animate text for questionLabel
    UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.5, options: nil, animations: {

        self.questionLabel.center = CGPoint(x: -110 , y: 305 + 20)

        }, completion: nil)

} else {

    performSegueWithIdentifier("countdownSegue", sender: self)

    //Add some logic here to run whenever the user has answered all the questions.

}

}

var countryPicker = ["France", "Germany", "Spain", "Northern Ireland", "Austria"]

var sortedArray = sorted(countryPicker)


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Hides the text field
questionTextField.hidden = true

questionTextField.delegate = self

// Sets the button text
buttonLabel.setTitle("Get started", forState: UIControlState.Normal)

// Sets the question text to be blank
questionLabel.text = ""

// Sets placeholder text in the text field
questionTextField.placeholder = ""

var pickerView = UIPickerView()

pickerView.delegate = self

questionTextField.inputView = pickerView


}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countryPicker.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return countryPicker[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
questionTextField.text = countryPicker[row]
}



// resigns the keyboard when user presses the return/next key on keyboard

func textFieldShouldReturn(textField: UITextField) -> Bool {

questionTextField.resignFirstResponder()

return true

}

// Resigns the keyboard if the user presses anywhere on the screen
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

self.view.endEditing(true)
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

2 个答案:

答案 0 :(得分:3)

对数组进行排序,或者创建数组的排序版本,然后使用该排序数组作为选择器视图的源。

let array = ["orange", "grape", "apple", "pear", "mango"]
let sortedArray = sorted(array)

然后,您将在数据源中使用sortedArray作为选择器视图。

答案 1 :(得分:0)

我设法得到了我正在寻找的:

countryPicker.sort(){$0 < $1}

它从A-Z对数组进行了排序,这是它对我有用的唯一方式。