从Enum获取数据以在UIPickerView上显示

时间:2015-05-04 08:11:29

标签: ios swift enums uipickerview

我正在创建一个简单的View,其中有一个UIPickerView,我想从枚举中显示PickerView上的选项。我创造了一种可能的饮料

enum drink{
  case coffee
  case tea
  case cola
  case water
}

我想知道如何在UIPickerView中显示饮料?我正在使用Swift 1.2和Xcode 6.3.1 ..谢谢!! :)

5 个答案:

答案 0 :(得分:5)

ABakerSmith所述,您可以使用Printable协议在String中显示UIPickerView

最后,您可以看到ViewController如下:

//
//  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)
}

答案 1 :(得分:4)

给它们一个String并用rawValue调用它。

enum Drink: String {
    case Coffee = "Coffee"
    case Tea = "Tea"
    case Cola = "Cola"
    case Water = "Water"
}

println(Drink.Coffee.rawValue) // Prints "Coffee"

正如ABakerSmith在他的回答中提到的,你也可以使用可打印的。在下一个例子中,与ABakerSmith的方法相比,它有点不同。

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"

正如莱昂纳多·萨维奥·达布斯所说,简化版本。

答案 2 :(得分:1)

我会Drink符合Printable协议:

Printable协议如下:

protocol Printable {
    var description: String { get }
}

因此,您的Drink枚举将是:

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"
        }
    }
}

以下是在Drink中使用String的示例:

let myDrink: Drink = .Tea
println("I'm drinking \(myDrink)") // I'm drinking Tea

答案 3 :(得分:1)

Apple在Swift 4.2 / Xcode 10+中引入了CaseIterable,这使它变得更干净。

  

对于任何枚举,编译器可以自动提供CaseIterable要求的实现,而其case不具有关联值或@available属性。合成的allCases集合按案例的顺序提供了案例。

示例:

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
    }
}

您可以在Github上使用UITextField找到完整的示例

答案 4 :(得分:0)

你可以这样使用枚举;

enum drink: String{
    case coffee = "coffee"
    case tea = "tea"
    case cola = "cola"
    case water = "water" 
}

您可以在此示例中添加uipickerview;

  

http://makeapppie.com/tag/uipickerview-in-swift/