我有一个UIPickerView,并希望通过mdCounty.name迭代我的行,但无法获得任何工作。如果没有,那么存储数据的更合适的元素是什么?
这是我的模型数据:
struct mdCounty {
var name: String?
var countyRecordation: Double = 0
var countyTransfer: Double = 0
var countyDeduction: Double = 0
}
let garrett = mdCounty(name: "Garrett", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 50000)
let allegany = mdCounty(name: "Allegany", countyRecordation: 6.50, countyTransfer: 0.005, countyDeduction: 30000)
let anneArundel = mdCounty(name: "Anne Arundel", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 0.0)
let baltimoreCity = mdCounty(name: "Baltimore City", countyRecordation: 10.0, countyTransfer: 0.015, countyDeduction: 22000)
let baltimoreCounty = mdCounty(name: "Baltimore County", countyRecordation: 5.0, countyTransfer: 0.015, countyDeduction: 22000)
let calvert = mdCounty(name: "Calvert", countyRecordation: 10.0, countyTransfer: 0.0, countyDeduction: 0.0)
let caroline = mdCounty(name: "Caroline", countyRecordation: 10.0, countyTransfer: 0.005, countyDeduction: 25000)
let cecil = mdCounty(name: "Cecil", countyRecordation: 8.20, countyTransfer: 0.0, countyDeduction: 0.0)
let charles = mdCounty(name: "Charles", countyRecordation: 10.0, countyTransfer: 0.01, countyDeduction: 0.0)
let dorchester = mdCounty(name: "Dorchester", countyRecordation: 10.0, countyTransfer: 0.0075, countyDeduction: 0.0)
控制器:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var userPrice = String?()
var userLoan = String?()
var userCredit = String?()
var split = TRUE
let titleData = TitleData()
var selectedCounty = String?()
@IBOutlet weak var closingCostLabel: UILabel!
@IBOutlet weak var salesPriceField: UITextField!
@IBOutlet weak var loanAmountField: UITextField!
@IBOutlet weak var sellerCreditField: UITextField!
@IBOutlet weak var costsLabel: UILabel!
@IBOutlet weak var countyPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
countyPicker.delegate = self
countyPicker.dataSource = self
view.backgroundColor = UIColor(red: 0xfd/255, green: 0xe8/255, blue: 0xd7/255, alpha: 1.0)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return mdCounty(name).count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent componefnt: Int) {
selectedCounty = mdCounty.name[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "\(titleData.mdCounty.name[row])"
}
答案 0 :(得分:3)
您真正想要使用的是Array
个mdCounty
个对象:
struct mdCounty {
var name: String = ""
var countyRecordation: Double = 0
var countyTransfer: Double = 0
var countyDeduction: Double = 0
}
let mdCounties: [mdCounty] = [
mdCounty(name: "Garrett", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 50000),
mdCounty(name: "Allegany", countyRecordation: 6.50, countyTransfer: 0.005, countyDeduction: 30000),
mdCounty(name: "Anne Arundel", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 0.0),
...
]
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return mdCounties.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedCounty = mdCounties[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return mdCounties[row].name
}