问题
I am creating a simple View where there is a UIPickerView and I want to display the choices on the PickerView from an enum. I have created an enum of possible drinks
enum drink{
case coffee
case tea
case cola
case water
}
I was wondering how it would be possible to display the drinks in an UIPickerView? I am using Swift 1.2 and Xcode 6.3.1.. Thanks!! :)
回答1:
As mentioned by ABakerSmith
you can use the Printable protocol to display the String
in the UIPickerView
.
In the end you can have a ViewController
looking as the following:
//
// ViewController.swift
// Test
//
// Created by Stefan Veis Pennerup on 04/05/15.
// Copyright (c) 2015 Kumuluzz. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var pickerView: UIPickerView!
enum Drink: Int, Printable {
case Coffee = 0
case Tea = 1
case Cola = 2
case Water = 3
static var count: Int { return Drink.Water.hashValue + 1 }
var description: String {
switch self {
case .Coffee: return "Coffee"
case .Tea : return "Tea"
case .Cola : return "Cola"
case .Water : return "Water"
default: return ""
}
}
}
// MARK: - UIPickerViewDataSource
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Drink.count.hashValue
}
// MARK: - UIPickerViewDelegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return Drink(rawValue: row)?.description;
}
// several optional methods:
// func pickerView(pickerView: UIPickerView!, widthForComponent component: Int) -> CGFloat
// func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat
// func pickerView(pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString!
// func pickerView(pickerView: UIPickerView!, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView!
// func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
}
回答2:
Give them a String and call it with rawValue.
enum Drink: String {
case Coffee = "Coffee"
case Tea = "Tea"
case Cola = "Cola"
case Water = "Water"
}
println(Drink.Coffee.rawValue) // Prints "Coffee"
As ABakerSmith mentioned in his answer, you can use printable as well. In the next example it's a little different compared to the approach of ABakerSmith's.
enum Drink: String, Printable {
case Coffee = "Coffee"
case Tea = "Tea"
case Cola = "Cola"
case Water = "Water"
var description: String { return rawValue }
}
println(Drink.Coffee) // Prints "Coffee"
As Leonardo Savio Dabus mentioned, a simplified version.
回答3:
I would make Drink
conform to the Printable
protocol:
The Printable
protocol looks like:
protocol Printable {
var description: String { get }
}
Therefore, your Drink
enum would be:
enum Drink : Printable {
case Coffee
case Tea
case Cola
case Water
var description: String {
switch self {
case .Coffee: return "Coffee"
case .Tea : return "Tea"
case .Cola : return "Cola"
case .Water : return "Water"
}
}
}
And here's an example of using a Drink
in a String
:
let myDrink: Drink = .Tea
println("I'm drinking \(myDrink)") // I'm drinking Tea
回答4:
Apple introduced CaseIterable in Swift 4.2 / Xcode 10+ which makes this cleaner.
The compiler can automatically provide an implementation of the CaseIterable requirements for any enumeration without associated values or @available attributes on its cases. The synthesized allCases collection provides the cases in order of their declaration.
Example:
enum Drink: String, CaseIterable {
case coffee = "Coffee"
case tea = "Tea"
case cola = "Cola"
case water = "Water"
}
extension ViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Drink.allCases[row].rawValue
}
}
extension ViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Drink.allCases.count
}
}
You can find a full example using a UITextField
on Github
回答5:
You can use enum like this way;
enum drink: String{
case coffee = "coffee"
case tea = "tea"
case cola = "cola"
case water = "water"
}
You can add uipickerview in this example;
http://makeapppie.com/tag/uipickerview-in-swift/
来源:https://stackoverflow.com/questions/30025481/take-data-from-enum-to-show-on-uipickerview