我很乐意帮助获得一个 swift 解决方案,这个解决方案与在Objective-C here中完成的代码类似'。
我基本上想要一个有两个组件的选择器。我希望能够在第一个组件中选择一行,并在此基础上选择一个列中的所选组件,它会在第二个组中显示相应数据的值。当搜索特定的足球俱乐部时,这将阻止无限卷轴。
数据:
Top Level:
leagueName.text
Second Level:
teamName.text
相关代码如下所示:
// 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
}
}