Swift:如何为 UIPickerView 中的行分配不同的数值?

Posted

技术标签:

【中文标题】Swift:如何为 UIPickerView 中的行分配不同的数值?【英文标题】:Swift: How to assign different number values to rows in a UIPickerView? 【发布时间】:2014-11-21 20:30:49 【问题描述】:

我有一个表示 UIPickerView 行的数组:

var exerciseArray = ["Sitting","Walking","Bowling","Biking Slow","Biking Fast","Golf","SoftBall","BaseBall","Weight Training","Ice Skating","Tennis","Basketball","Jogging","Swimming","Soccer","Jump Rope"]

如何为var exerciseArray 中的每个不同行分配不同的数字值,以便稍后访问这些分配的数字进行计算?

我是编程新手,所以请提供尽可能多的代码。谢谢!

【问题讨论】:

【参考方案1】:
struct Model 
    var name: String
    var number: Int 
    

var exersise : [Model] = []

var Position1 = Model(name: "Sitting", number: 100)  
var Position2 = Model(name: "Walking", number: 130)  
var Position3 = Model(name: "Bowling", number: 160)  
var Position4 = Model(name: "Biking", number: 200)  
var Position5 = Model(name: "Golf", number: 300)

exersise = [Position1,Position2,Position3,Position4,Position5]


println(exersise[0].name)  
println(exersise[0].number)



for var index = 0; index < exersise.count; index++ 
    println("Name = \(exersise[index].name) and has the number = \(exersise[index].number)")  

【讨论】:

好吧,我不确定协议方法 pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) 是否会在返回时接受 Model 作为数据类型。

以上是关于Swift:如何为 UIPickerView 中的行分配不同的数值?的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UIPickerView 添加边框?

ios swift 2如何用json数据填充uipickerview [重复]

Swift 3:滚动视图中的 UIPickerView

如何为 Data swift 添加扩展?

UIPickerView + TableViewCell + 委托和数据源

Swift 3 中的 UIPickerView 和子类数组