如何将列添加到预先存在的 uipickerview
Posted
技术标签:
【中文标题】如何将列添加到预先存在的 uipickerview【英文标题】:how to add a column to pre-exisiting pickerview 【发布时间】:2018-01-16 14:36:05 【问题描述】:我需要一个多组件选择器视图,但我需要在它的最左侧添加一个新列。到目前为止,它只有我需要的三个中的两个。这是当前代码。我添加了数组,将数组包装在数组中,将变量添加为列并更改了输出。
import UIKit
class Country
var cats: String
var country: String
var cities: [String]
init(country:String, cities:[String])
self.cats = cat
self.country = country
self.cities = cities
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var countryLbl: UILabel!
var countries = [Country]()
override func viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
cats.apppend(Cat(cat: "furry",
countries.append(Country(country: "India", cities: ["Delhi", "Ahmedabad", "Mumbai", "Pune"]))
countries.append(Country(country: "USA", cities: ["New York", "DC", "Fairfax"]))
countries.append(Country(country: "Austrailia", cities: ["Sydney", "Melbourne"]))
super.viewDidLoad()
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 3
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if component == 0
return countries.count
else
let selectedCountry = pickerView.selectedRow(inComponent: 0)
return countries[selectedCountry].cities.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if component == 0
return countries[row].country
else
let selectedCountry = pickerView.selectedRow(inComponent: 0)
return countries[selectedCountry].cities[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
pickerView.reloadComponent(1)
let selectedCountry = pickerView.selectedRow(inComponent: 0)
let selectedCity = pickerView.selectedRow(inComponent: 1)
let country = countries[selectedCountry].country
let city = countries[selectedCountry].cities[selectedCity]
countryLbl.text = "Country: \(country)\nCity: \(city)"
我添加了变量,但我是否添加了更多括号,然后我不知道如何识别自我,例如为什么国家 = 国家但猫不能 = 猫?另外,我要添加的第一列(行的最左侧)只有 2 个选项,不会影响其他选项。
【问题讨论】:
你需要什么第三列?是什么阻止您添加第三列? 所以有两个,但我需要在开头添加一个(总共有 3 个列)第一个(我需要的列)不影响其他两个只是一个简单的 2 个选择答案 那么是什么阻止您添加新列?你试过什么?你有什么问题? 是的,您需要更新每个选择器视图数据源和委托方法以使用新组件。试试看。然后用您尝试的代码更新您的问题并解释您遇到的问题。 只要解释一下你想让你的选择器视图做什么。不要担心它已经做了什么。它已经做了什么是无关紧要的。你想让它做什么? 【参考方案1】:由于cats
组件与国家/地区无关,请不要将cats
添加到您的Countries
类(顺便说一下,应该是struct
)。
只需在视图控制器中有一个单独的 cats
数组。而且你需要更新所有的选择器视图方法。
struct Country
let country: String
let cities: [String]
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var countryLbl: UILabel!
let cats = ["Cat1", "Cat2"]
var countries = [Country]()
override func viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
countries.append(Country(country: "India", cities: ["Delhi", "Ahmedabad", "Mumbai", "Pune"]))
countries.append(Country(country: "USA", cities: ["New York", "DC", "Fairfax"]))
countries.append(Country(country: "Austrailia", cities: ["Sydney", "Melbourne"]))
super.viewDidLoad()
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 3
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if component == 0
return cats.count
else if component == 1
return countries.count
else
let selectedCountry = pickerView.selectedRow(inComponent: 1)
return countries[selectedCountry].cities.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if component == 0
return cats[row]
else if component == 1
return countries[row].country
else
let selectedCountry = pickerView.selectedRow(inComponent: 1)
return countries[selectedCountry].cities[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if component == 0
// do something with the new cat
else if component == 1 || component == 2
if component == 1
pickerView.reloadComponent(2)
let selectedCountry = pickerView.selectedRow(inComponent: 1)
let selectedCity = pickerView.selectedRow(inComponent: 2)
let country = countries[selectedCountry].country
let city = countries[selectedCountry].cities[selectedCity]
countryLbl.text = "Country: \(country)\nCity: \(city)"
【讨论】:
爱你 2 飞快的月亮和回来 等待这没有意义。你为什么要在底部代码中添加 cat ?这不会弄乱其他两列吗?从“func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 开始 是的,这很有意义。正如我在我的几个 cmets 中所述并在我的回答中清楚地表明,您必须更新所有选择器视图方法以使用所有三个组件。你真的按原样尝试了我的代码吗?我的代码将在选择器视图中显示三个组件。猫列表将在第一个组件中。中间部分的国家列表。最后一个组件中的城市列表。这就是你想要的,对吧? 这是一个与这个问题完全不同的问题。表示这个问题已经解决了。如果您在新问题上需要帮助,请发布一个包含所有相关详细信息(包括完整错误)的新问题。以上是关于如何将列添加到预先存在的 uipickerview的主要内容,如果未能解决你的问题,请参考以下文章