问题
I am would be greatful for some help in getting a swift solution of something 'similar' to this code which was done in Objective-C here.
I basically want a picker with two components. I would like to be able to select a row in first component and on that basis of selected component in one column it shows the value of the corresponding data in the second. This would stop the endless scroll when searching for a specific football club.
Data:
Top Level:
leagueName.text
Second Level:
teamName.text
Relevant code looks like this:
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITextViewDelegate, UIPickerViewDelegate {
@IBOutlet weak var homeTeamNameTextView: UITextView!
@IBOutlet weak var awayTeamNameTextView: UITextView!
@IBOutlet weak var teamNamesPicker: UIPickerView! = UIPickerView()
var activeTextView: UITextView?
// Array of teams (a bit large needs to fix... [ EPL ] is not a teamName but leagueName followed by leagueNames)
let teamNamesArray = ["[ EPL ]", "Arsenal", "Aston Villa", "Burnley","Chelsea", "Crystal Palace", "Everton", "Hull City", "Leicester City", "Liverpool", "Manchester City", "Manchester United", "Newcastle United", "Queens Park Rangers", "Southampton", "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur", "West Bromwich Albion", "West Ham United", "[ LFP ]", "Almería", "Athletic Bilbao", "Atlético Madrid", "Barcelona", "Celta de Vigo", "Córdoba", "Deportivo La Coruña", "Eibar", "Elche", "Espanyol", "Getafe", "Granada", "Levante", "Málaga", "Rayo Vallecano", "Real Madrid", "Real Sociedad", "Sevilla", "Valencia", "Villarreal", "[ BUNDESLIGA ]", "Augsburg", "Bayer Leverkusen", "Bayern München", "Borussia Dortmund", "Borussia M'gladbach", "Eintracht Frankfurt", "Freiburg", "Hamburger SV", "Hannover", "Hertha BSC", "Hoffenheim", "Mainz", "Paderborn", "Schalke", "Stuttgart", "Werder Bremen", "Wolfsburg", "[ SERIE A ]", "Atalanta", "Cagliari", "Cesena", "Chievo", "Empoli", "Fiorentina", "Genoa", "Hellas Verona", "Internazionale", "Juventus", "Lazio", "Milan", "Napoli", "Palermo", "Parma", "Roma", "Sampdoria", "Sassuolo", "Torino", "Udinese", "[ LIGUE 1 ]", "Bastia", "Bordeaux", "Caen", "Évian", "Guingamp", "Lens", "Lille", "Lorient", "Lyon", "Marseille", "Metz", "Monaco", "Montpellier", "Nantes", "Nice", "Paris Saint-Germain", "Reims", "Rennes", "Saint-Étienne", "Toulouse", "[ A-LEAGUE ]", "Adelaide United", "Brisbane Roar", "Central Coast Mariners", "Melbourne City", "Melbourne Victory", "Newcastle Jets", "Perth Glory", "Sydney FC", "Wellington Phoenix", "Western Sydney Wanderers"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
teamNamesPicker.hidden = true;
teamNamesPicker.delegate = self
homeTeamNameTextView.text = "Select Home Team"
homeTeamNameTextView.delegate = self
awayTeamNameTextView.text = "Select Away Team"
awayTeamNameTextView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return teamNamesArray.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return teamNamesArray[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
activeTextView?.text = teamNamesArray[row]
teamNamesBackground.hidden = true
teamNamesPicker.hidden = true
return
}
func textViewShouldBeginEditing(textView: UITextView) -> Bool{
teamNamesBackground.hidden = false
teamNamesPicker.hidden = false
activeTextView = textView
return false
}
}
来源:https://stackoverflow.com/questions/27639282/uipickerview-hierarchy-levels