在我的应用中,我试图在事件->重复->自定义屏幕上为两个PickerViews重新创建与本机iOS日历应用中相同的行为逻辑。
我发现,我可以更改整个2d Picker,具体取决于1st Picker的值。只需在2d Picker的单元格中制作几个Picker,并在其2d组件中使用不同的文本,然后根据第一个Picker的isHidden = true
将它们设为false
或selectedRow
。因此,如果用户在第一个选择器中选择了“每日”,我将在第一个部分中显示2d Picker,在第一个部分中显示天数,在第二个部分中显示文本“ days”,在其他第二个选择器中显示(第二个部分中包含“ weeks”和“ moths”)-隐。对我来说看起来不太简洁,但可以使用,因为当用户与第一个Picker交互时,第二个Picker不可见。
但是我不知道如何在2d Picker中使2d组件的文本依赖于同一选择器中1st组件的值。正如我们在日历中看到的那样,当用户在第一个组件中选择“ 1”时,在第二个中他看到“天”,但是当他选择的数字大于1时,他在第二个组件中看到“天”。加载视图后,无法找到如何更改选择器的标题。
我如何使其起作用?非常期待阅读您的宝贵建议。
Screenshot of this Event -> Repeat -> Custom screen in Calendar app
UPD:
问题已通过.reloadComponent(Int)
解决。
因此,我有一些代码,希望对您有所帮助。 :)它还包含用于更新两个Picker单元格右侧的标题以及更新页脚中文本的逻辑,所有这些都与Calendar中的相同。
class CustomRepeatTableViewController: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var frequencyPicker: UIPickerView!
@IBOutlet weak var frequencyLabel: UILabel!
@IBOutlet weak var frequensyNumberPicker: UIPickerView!
@IBOutlet weak var frequencyNumberLabel: UILabel!
let frequencyPickerData = [
["Minutely", "Hourly","Daily","Weekly"]
]
var frequencyNumberPickerData = [
["1", "2", "3", "4", "5"]
]
let units = ["minute", "hour", "day", "week"]
func updatePicker(_ pickerView: UIPickerView, _ component: Int, _ row: Int) {
let unit = units[frequencyPicker.selectedRow(inComponent: 0)]
if pickerView == frequencyPicker {
frequencyNumberPickerData.remove(at: 1)
if frequensyNumberPicker.selectedRow(inComponent: 0) == 0 {
frequencyNumberPickerData.append([unit])
} else {
frequencyNumberPickerData.append([unit + "s"])
}
frequensyNumberPicker.reloadComponent(1)
} else {
if component == 0 && row == 0 {
frequencyNumberPickerData.remove(at: 1)
frequencyNumberPickerData.append([unit])
frequensyNumberPicker.reloadComponent(1)
} else {
frequencyNumberPickerData.remove(at: 1)
frequencyNumberPickerData.append([unit + "s"])
frequensyNumberPicker.reloadComponent(1)
}
}
}
func updateLabels() {
let selectedRow = frequencyPicker.selectedRow(inComponent: 0)
let unit = units[selectedRow]
let frequencyNumber = frequencyNumberPickerData[0][frequensyNumberPicker.selectedRow(inComponent: 0)]
var text: String
var footerText: String
let frequency = frequencyPickerData[0][selectedRow]
frequencyLabel.text = frequency
if frequensyNumberPicker.selectedRow(inComponent: 0) > 0 {
text = frequencyNumber + " " + unit + "s"
footerText = frequencyNumber + " " + unit + "s."
} else {
text = unit.capitalized
footerText = unit + "."
}
frequencyNumberLabel.text = text
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 230, height: 30))
let label = UILabel(frame: CGRect(x:20, y:0, width:230, height:30))
label.text = "Repeat will occur every " + footerText
label.font = label.font.withSize(13)
label.textColor = UIColor.darkGray
tableView.tableFooterView?.addSubview(label)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if pickerView == frequencyPicker {
return frequencyPickerData.count
} else {
return frequencyNumberPickerData.count
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == frequencyPicker {
return frequencyPickerData[component].count
} else {
return frequencyNumberPickerData[component].count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == frequencyPicker {
return frequencyPickerData[component][row]
} else {
return String(frequencyNumberPickerData[component][row])
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
updatePicker(pickerView, component, row)
updateLabels()
}
override func viewDidLoad() {
super.viewDidLoad()
frequencyPicker.delegate = self
frequencyPicker.dataSource = self
frequencyPicker.selectRow(1, inComponent: 0, animated: true) //to make selection separators appear
frequensyNumberPicker.delegate = self
frequensyNumberPicker.dataSource = self
frequensyNumberPicker.selectRow(0, inComponent: 0, animated: true) //to make selection separators appear
updateLabels()
frequencyNumberPickerData.append([units[frequencyPicker.selectedRow(inComponent: 0)]])
}
}