我搜索过(并发现了一些使用静态数组),但是找不到与我尝试使用JSON填充结构相关的任何内容。
我是Swift和StackExchange的新手,所以如果我错过了,请告诉我!
我有三个结构从JSON代码填充。 注意:departmentName与其他两个结构中的Department相同
struct Dept: Decodable {
let departmentName: String
}
struct Company: Decodable {
let companyName: String
let Department: String
}
struct Spread: Decodable {
let spreadName: String
let spreadType: String
let Department: String
}
这些加载在viewDidLoad上。 数组(ish)形式的数据示例如下:
Department = "Department 1"
Company = ["Company 1":"Department 1","Company 2":"Department 2"]
Spread = ["Spread 1":"Department 1","Spread 2 ":"Department 2"]
我目前的PickerView功能代码:
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countRows : Int = dep.count
if pickerView == companyPicker {
countRows = self.company.count
}
else if pickerView == spreadPicker {
countRows = self.spread.count
}
else if pickerView == incidentPicker {
countRows = self.incident.count
}
return countRows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == deptPicker {
let titleRow = dep[row].departmentName
return titleRow
}
else if pickerView == companyPicker {
let titleRow = company[row].companyName
return titleRow
}
else if pickerView == spreadPicker {
let titleRow = spread[row].spreadName
return titleRow
}
else if pickerView == incidentPicker {
let titleRow = incident[row].incidentTypeName
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == deptPicker {
self.deptartmentTextField.text = self.dep[row].departmentName
self.deptartmentTextField.resignFirstResponder()
}
else if pickerView == companyPicker {
self.companyTextField.text = self.company[row].companyName
self.companyTextField.resignFirstResponder()
}
else if pickerView == spreadPicker {
self.spreadTextField.text = self.spread[row].spreadName
self.spreadTextField.resignFirstResponder()
}
else if pickerView == incidentPicker {
self.incidentTextField.text = self.incident[row].incidentTypeName
self.incidentTextField.resignFirstResponder()
}
}
我想我最大的问题是,是否有办法根据部门选择器过滤公司和点差选择器。
答案 0 :(得分:0)
为了解决这个问题,我添加了两个过滤第一个部门选择器中数据的函数。我将这些添加到我的didSelectRow选择器函数中。然后我在过滤后重新加载了第二个和第三个选择器。