PickerView 没有从 Array 获取更新的值
Posted
技术标签:
【中文标题】PickerView 没有从 Array 获取更新的值【英文标题】:PickerView not getting the updated value from Array 【发布时间】:2021-02-16 15:49:26 【问题描述】:首先请更正我的主题或建议对标签进行编辑,以防我提到错误。
我正在开发一个商店应用程序项目,但由于 UIPickerView 没有从目标数组中获取更新的值,因此我卡在了选择 subSection VC 中!
下面是我的代码
class SelectSectionVC : UIViewController, UITextFieldDelegate
var delegate : SelectSectionDelegate!
var staticVariable = SelectionCell.shared
override func viewDidLoad()
super.viewDidLoad()
getSection()
setupCell()
self.pickertextField.alpha = 0
self.DoneBtn.isHidden = true
self.pickerView.isHidden = true
pickertextField.addTarget(self, action: #selector(textField_CatchedData(_:)), for: .editingDidEnd )
@IBOutlet weak var CollectionView: UICollectionView!
@IBOutlet weak var pickerView: UIPickerView!
didSet pickerView.delegate = self ; pickerView.dataSource = self
var selectedSection : SectionsObject?
var sectionsArray : [SectionsObject] = []
func getSection()
SectionsAPI.getAllsections (section) in
self.sectionsArray.append(section)
DispatchQueue.main.async
self.CollectionView.reloadData()
// Products type Arrays
var type_FP : [String] = ["FP1", "FP2"]
var type_TP : [String] = ["TP1", "TP2"]
var type_JW = ["Rings", "Necklace", "Collar", "Bracelet", "Earring"]
var type_ACS = ["Hair Wrap", "Ring", "Strap", "Sunglasses", "Crown"]
var type_LP = ["Waist belt", "Wallet", "Handbag", "Suitcase", "Face Mask"]
var type_Watches = ["classic", "Leather", "Smart", "Digital"]
var generatedTypes : [String] = [] didSetprint("@ Types updated ==> ( \(generatedTypes) )")
func updateTypesarray()
if selectedSection?.name == "Trending Products"
self.generatedTypes = type_TP
else if selectedSection?.name == "Accessories"
self.generatedTypes = type_ACS
else if selectedSection?.name == "Featured Products"
self.generatedTypes = type_FP
else if selectedSection?.name == "Watches"
self.generatedTypes = type_Watches
else if selectedSection?.name == "Jewellery Products"
self.generatedTypes = type_JW
else if selectedSection?.name == "Leather Products"
self.generatedTypes = type_LP
else print("@ ((Nothing Happaned!!))")
@IBOutlet weak var pickertextField: UITextField!
didSet
pickertextField.inputView = UIView() //what a Magic!!!... This code solved Keyboard dismiss problem after assign the textField as FirstResponder!
pickertextField.delegate = self
pickertextField.allowsEditingTextAttributes = false
pickertextField.becomeFirstResponder()
@objc func textField_CatchedData(_ sender : UITextField)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Type Name"), object: nil, userInfo: ["text" : sender.text!])
var productsList: [productObject] = []
var RecommendedArray: [String] = ["TH-1791721_side","Image-2","touq2","TH-1791349","watch2","eswara","Image-1",] //to be updated later!
extension SelectSectionVC : UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
func setupCell()
CollectionView.delegate = self; CollectionView.dataSource = self
CollectionView.register(UINib(nibName: "SelectionCell", bundle: nil), forCellWithReuseIdentifier: "cell")
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
return CGSize(width: self.CollectionView.frame.size.width - 25, height: self.CollectionView.frame.size.height/4 - 0.5)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat // make spacing between each cell
return 2
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
sectionsArray.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = CollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SelectionCell
cell.isSelected = false
pickertextField.text = "Select product type" // Reset text to "Select product type" when press new cell"
cell.sectionName.text = sectionsArray[indexPath.row].name
return cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
print("@ secArray Count = \(sectionsArray.count)")
self.selectedSection = sectionsArray[indexPath.row]
updateTypesarray()
print("@ //Selected cell => TypesArray => \(generatedTypes)")
pickertextField.becomeFirstResponder()
问题是:组件中的标题和行数在选择单元格后更新后没有从(生成的类型)数组中获取值!
extension SelectSectionVC : UIPickerViewDelegate, UIPickerViewDataSource
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return generatedTypes.count
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?
return NSAttributedString(string: self.generatedTypes[row], attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return generatedTypes[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
self.pickertextField?.text = generatedTypes[row]
self.pickerView.isHidden = true
self.DoneBtn.isHidden = false
self.pickertextField.endEditing(true)
print("# pickerTextField... has ended editing!")
我做了很多断点,我确信 [generatedTypes] 数组有一个值..但我不知道为什么它总是在更新之前在 pickerView 中调用!
请帮帮我
提前谢谢你
【问题讨论】:
【参考方案1】:您可能需要重新加载updateTypesarray
的选择器端
func updateTypesarray()
......
self.pickerView.reloadAllComponents()
【讨论】:
非常感谢......它奏效了,你挽救了我的生命......你能投票支持可能的问题吗?以上是关于PickerView 没有从 Array 获取更新的值的主要内容,如果未能解决你的问题,请参考以下文章