gpt4 book ai didi

arrays - 如何将列添加到预先存在的 uipickerview

转载 作者:行者123 更新时间:2023-11-28 07:55:20 26 4
gpt4 key购买 nike

我需要一个多组件选择器 View ,但我需要在它的最左侧添加一个新列。到目前为止,它只有我需要的三个中的两个。这是当前代码。我添加了数组,将数组包装在数组中,将变量添加为列,还更改了输出。

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)"

}
}

我添加了变量,但我是否添加了更多括号,然后我不知道如何识别 self ,比如为什么 country = country 但 cat 不能 = cat?此外,我要添加的第一列(行的最左侧)将只有 2 个选择,不会影响其他选择。

最佳答案

因为 cats 组件与国家无关,所以不要将 cats 添加到您的 Countries 类(它应该是一个 struct,顺便说一句)。

只需在您的 View Controller 中有一个单独的 cats 数组。并且您需要更新所有选择器 View 方法。

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)"
}
}
}

关于arrays - 如何将列添加到预先存在的 uipickerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48283654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com