PickerView 没有从 Array 获取更新的值

PickerView not getting the updated value from Array

首先,请更正我的主题或建议对标签进行编辑,以防我提到的错误。

我正在开发一个商店应用程序项目,但由于 UIPickerView 未从目标数组获取更新值,我卡在了选择子部分 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] = []  { didSet{print("@ 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()
                                    
}

}

问题是:当单元格被选中时,标题和组件中的行数无法从 (generatedTypes) 数组中获取值!

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 更新之前调用!

请帮帮我

提前致谢

您可能需要重新加载 updateTypesarray

的选择器端
func updateTypesarray(){
  ......
  self.pickerView.reloadAllComponents()
}