如何清除两个 pickerView 中的一个?
How can I clear one of pickerView from two?
我有两个 pickerView,当用户 select 在加载第二个 pickerView 之后。但是如果用户再次执行此操作,所有数据都会附加到第二个 pickerView。当用户 select 第一个时,我必须清除第二个 pickerView 内容。我正在使用 reloadAllComponents() 方法,但它不起作用。
我的代码;
var country_array = [String]()
var countryIds_array = [String]()
var city_array = [String]()
var cityIds_array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
getCountry()
}
func getCountry(){
//var hata = false
print("Getting Country List")
let query = PFQuery(className: "cls_ulke")
query.limit = 300
query.order(byDescending: "oncelik")
query.findObjectsInBackground { (objects: [PFObject]?, error) -> Void in
if error == nil {
print("Getted!")
print("Looping------------------>")
if let objects = objects {
for object in objects{
print(object.objectId ?? "BOŞ")
print(object["ulke_adi"])
self.country_array.append(object["ulke_adi"] as! String)
self.countryIds_array.append(object.objectId!)
}
self.pckerview.delegate = self
self.pckerview.dataSource = self
}
print("<-------------------Looped")
}else{
print("HATA")
}
}
}
func getCities(objectID:String){
print("Getting City List")
let query = PFQuery(className: "cls_il")
query.limit = 1000
query.order(byDescending: "oncelik")
query.whereKey("ulke_ID", equalTo: PFObject(withoutDataWithClassName: "cls_ulke", objectId: objectID))
query.findObjectsInBackground { (objects:[PFObject]?, error) in
if error == nil {
print("Getted!")
print("Looping------------------>")
if let objects = objects {
for object in objects {
print(object["il_adi"])
self.city_array.append(object["il_adi"] as! String)
self.cityIds_array.append(object.objectId!)
}
self.pckerview2.reloadAllComponents()
self.pckerview2.delegate = self
self.pckerview2.dataSource = self
}
print("<-------------------Looped")
}else{
print("HATA")
}
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var donecek = 0
if pickerView == pckerview {
donecek = country_array.count
}else if pickerView == pckerview2{
donecek = city_array.count
}
return donecek
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var donecek = ""
if pickerView == pckerview {
donecek = country_array[row]
}else if pickerView == pckerview2{
donecek = city_array[row]
}
return donecek
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pckerview {
tfield.text = countryIds_array[row]
getCities(objectID:countryIds_array[row])
}
}
reloadAllComponents()
正在呼叫您的代表以重新加载。来自 documentation:
Calling this method causes the picker view to query the delegate for new data for all components.
但是,您的代码显示您只是在其后设置 pckerview2.delegate
。
self.pckerview2.reloadAllComponents()
self.pckerview2.delegate = self
self.pckerview2.dataSource = self
所以我的猜测是 reloadAllCompontents()
没有设法查询代理,因为它是 nil
。(除非你在其他地方设置它)尝试像这样将它移动到下面:
self.pckerview2.delegate = self
self.pckerview2.dataSource = self
self.pckerview2.reloadAllComponents()
第二种可能性是您没有清除 city_array
因此您总是在其后附加城市。如果是这种情况,在 for
循环之前添加一个 removeAll()
。像这样:
print("Looping------------------>")
if let objects = objects {
self.city_array.removeAll()
self.cityIds_array.removeAll()
for object in objects {
// rest of the codes
我有两个 pickerView,当用户 select 在加载第二个 pickerView 之后。但是如果用户再次执行此操作,所有数据都会附加到第二个 pickerView。当用户 select 第一个时,我必须清除第二个 pickerView 内容。我正在使用 reloadAllComponents() 方法,但它不起作用。
我的代码;
var country_array = [String]()
var countryIds_array = [String]()
var city_array = [String]()
var cityIds_array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
getCountry()
}
func getCountry(){
//var hata = false
print("Getting Country List")
let query = PFQuery(className: "cls_ulke")
query.limit = 300
query.order(byDescending: "oncelik")
query.findObjectsInBackground { (objects: [PFObject]?, error) -> Void in
if error == nil {
print("Getted!")
print("Looping------------------>")
if let objects = objects {
for object in objects{
print(object.objectId ?? "BOŞ")
print(object["ulke_adi"])
self.country_array.append(object["ulke_adi"] as! String)
self.countryIds_array.append(object.objectId!)
}
self.pckerview.delegate = self
self.pckerview.dataSource = self
}
print("<-------------------Looped")
}else{
print("HATA")
}
}
}
func getCities(objectID:String){
print("Getting City List")
let query = PFQuery(className: "cls_il")
query.limit = 1000
query.order(byDescending: "oncelik")
query.whereKey("ulke_ID", equalTo: PFObject(withoutDataWithClassName: "cls_ulke", objectId: objectID))
query.findObjectsInBackground { (objects:[PFObject]?, error) in
if error == nil {
print("Getted!")
print("Looping------------------>")
if let objects = objects {
for object in objects {
print(object["il_adi"])
self.city_array.append(object["il_adi"] as! String)
self.cityIds_array.append(object.objectId!)
}
self.pckerview2.reloadAllComponents()
self.pckerview2.delegate = self
self.pckerview2.dataSource = self
}
print("<-------------------Looped")
}else{
print("HATA")
}
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var donecek = 0
if pickerView == pckerview {
donecek = country_array.count
}else if pickerView == pckerview2{
donecek = city_array.count
}
return donecek
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var donecek = ""
if pickerView == pckerview {
donecek = country_array[row]
}else if pickerView == pckerview2{
donecek = city_array[row]
}
return donecek
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pckerview {
tfield.text = countryIds_array[row]
getCities(objectID:countryIds_array[row])
}
}
reloadAllComponents()
正在呼叫您的代表以重新加载。来自 documentation:
Calling this method causes the picker view to query the delegate for new data for all components.
但是,您的代码显示您只是在其后设置 pckerview2.delegate
。
self.pckerview2.reloadAllComponents()
self.pckerview2.delegate = self
self.pckerview2.dataSource = self
所以我的猜测是 reloadAllCompontents()
没有设法查询代理,因为它是 nil
。(除非你在其他地方设置它)尝试像这样将它移动到下面:
self.pckerview2.delegate = self
self.pckerview2.dataSource = self
self.pckerview2.reloadAllComponents()
第二种可能性是您没有清除 city_array
因此您总是在其后附加城市。如果是这种情况,在 for
循环之前添加一个 removeAll()
。像这样:
print("Looping------------------>")
if let objects = objects {
self.city_array.removeAll()
self.cityIds_array.removeAll()
for object in objects {
// rest of the codes