因此,我试图从数组中填充两个选择器视图,但只想出“?”我在这里尝试过一些类似类型的修复,但是没有一个成功。
@IBOutlet var startPickerView: UIPickerView!
@IBOutlet var endPickerView: UIPickerView!
var startPickerData: [Int] = []
var endPickerData: [Int] = []
let startArray = (1...999).map { $0 }
let endArray = (2...1000).map { $0 }
let startPickerCellIndexPath = IndexPath(row: 1, section: 1)
let endPickerCellIndexPath = IndexPath(row: 1, section: 2)
var isStartPickerShown: Bool = false {
didSet {
startPickerView.isHidden = !isStartPickerShown
}
}
var isEndPickerShown: Bool = false {
didSet {
endPickerView.isHidden = !isEndPickerShown
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.startPickerView.delegate = self
self.startPickerView.dataSource = self
self.endPickerView.delegate = self
self.endPickerView.dataSource = self
startPickerData = startArray
endPickerData = endArray
self.startPickerView.reloadAllComponents()
self.endPickerView.reloadAllComponents()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath {
case startPickerCellIndexPath:
if isStartPickerShown {
return 216.0
} else {
return 0.0
}
case endPickerCellIndexPath:
if isEndPickerShown {
return 216.0
} else {
return 0.0
}
default:
return 44.0
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath {
case startPickerCellIndexPath:
if isStartPickerShown {
isStartPickerShown = false
} else if isStartPickerShown {
isStartPickerShown = false
isStartPickerShown = true
} else {
isStartPickerShown = true
}
tableView.beginUpdates()
tableView.endUpdates()
case endPickerCellIndexPath:
if isEndPickerShown {
isEndPickerShown = false
} else if isEndPickerShown {
isEndPickerShown = false
isEndPickerShown = true
} else {
isEndPickerShown = true
}
tableView.beginUpdates()
tableView.endUpdates()
default:
break
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return startPickerData.count
} else {
return endPickerData.count
}
}
// The data to return fopr the row and component (column) that's being passed in
private func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int? {
let startView: Int = startPickerData[row]
let endView: Int = endPickerData[row]
if pickerView.tag == 1 {
return startView
} else {
return endView
}
}
}
我已经花了一天半的时间来解决这个问题,还没有弄清楚为什么它不能工作,我已经尝试了几个修复方法。
您需要将最底层的私有方法更改为:
// The data to return fopr the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let startView: Int = startPickerData[row]
let endView: Int = endPickerData[row]
if pickerView.tag == 1 {
return String(format: "%d", startView)
} else {
return String(format: "%d", endView)
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。