我正在创建具有UIDatePicker
,UIPickerView
和TableView
的汽车的高级搜索,并且我遇到问题titleForRow。如果我隐藏一个选择器视图,我可以正确查看数据(得分)并看到另一个选秀视图(结果)。但是在我打开第二个选秀视图(结果)并打开另一个选秀视图(得分)而没有隐藏第一个选手时,当我尝试滚动得分选择器时我在titleForRow上有错误说& #34;数组索引超出范围"。那么,如果我要在一个视图控制器上使用多个pickerview?我如何以正确的方式连接它们?请帮助吗?
这是我的2picker和1date选择器的设计,
正如您所看到的,当我点击一个静态表格单元格时,选择器出现,当我再次点击时,选择器隐藏。
这是我的高级搜索控制器
这是我用来控制我的pickerview和静态表格单元格的代码
var pickerCollection : String = "Score"
override func viewDidLoad() {
TegKeychain.set("currentView", value: "Advance")
super.viewDidLoad()
datePickerChanged()
//self.scorePicker?.delegate = self
scoreDetailLabel.text = scoresValues[0]
resultDetailLabel.text = resultsValues[0]
detailLabel.text = venue
}
override func viewDidAppear(animated: Bool) {
TegKeychain.set("currentView", value: "Advance")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 && indexPath.row == 0 {
toggleDatepicker()
}
if indexPath.section == 2 && indexPath.row == 0{
pickerCollection = "Score"
toggleScorePicker()
scorePicker.reloadAllComponents()
}
if indexPath.section == 2 && indexPath.row == 2{
pickerCollection = "Result"
toggleResultPicker()
resultPicker.reloadAllComponents()
}
println("Section : \(indexPath.section)")
println("Row : \(indexPath.row)")
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if datePickerHidden && indexPath.section == 0 && indexPath.row == 1 {
return 0
}
else if scorePickerHidden && indexPath.section == 2 && indexPath.row == 1{
return 0
}
else if resultPickerHidden && indexPath.section == 2 && indexPath.row == 3{
return 0
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerCollection == "Score"{
return scoresValues.count
}
if pickerCollection == "Result"{
return resultsValues.count
}
return 0
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerCollection == "Score"{
return scoresValues[row]
}
if pickerCollection == "Result"{
return resultsValues[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerCollection == "Score"{
scoreDetailLabel.text = scoresValues[row]
}
if pickerCollection == "Result"{
resultDetailLabel.text = resultsValues[row]
}
}
// I will not show Date Picker to make it simple.
结果选择器和分数选择器
/////////////////////////////////////////////////////// Scores ///////////////////////////////////////////////////////////////////
var scorePickerHidden = true
var scores :[String:String] = ["":"All","S":"S","R":"R","RA":"RA","above 5":"Above 5","above 4":"Above 4","above 3":"Above 3","above 2":"Above 2","above 1":"Above 1","above 0":"Above 0"]
var scoresValues : [String] = ["All","S","R","RA","Above 5","Above 4","Above 3","Above 2","Above 1","Above 0"]
func toggleScorePicker() {
scorePickerHidden = !scorePickerHidden
tableView.beginUpdates()
tableView.endUpdates()
}
/////////////////////////////////////////////////////// Results ///////////////////////////////////////////////////////////////////
var resultPickerHidden = true
var results :[String:String] = ["":"All","not yet":"Not Yet","unsold":"Unsold","sold by nego":"Sold by nego","others":"Others"]
var resultsValues : [String] = ["All","Not Yet","Unsold","Sold by nego","Others"]
func toggleResultPicker() {
resultPickerHidden = !resultPickerHidden
tableView.beginUpdates()
tableView.endUpdates()
}
请给我任何帮助和建议?我不能只使用单个选择器,因为它不符合我的设计。那么,有没有其他方法,我可以在适当的静态单元被轻敲时连接我正确的选择器视图?< / p>
答案 0 :(得分:0)
如果要使用多个选择器视图,则必须在Attributes Inspector中使用tag属性。所以,假设我们有两个UIPickerView
。在属性检查器中,将每个UIPickerView设置为标记#1和另一个#2。确保它们都连接到同一视图控制器的委托和数据源。
然后在你的代码中,
var scoresValues = []
var resultsValues = []
override func viewDidLoad(){
super.viewDidLoad()
scoreValues = ["a","b",...]
resultsValue = ["c","d",...]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1{
return scoresValues.count
}
else{
return resultsValues.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView.tag == 1{
return scoresValues[row]
}
else{
return resultsValues[row]
}
}
我所展示的仅使用2个选择器。如果你想使用更多的选择器,你可以使用if / else或switch来处理它们。