First of all please correct my topic subject or suggest an edit for the taggin in case if I mentioned somthing wrong.
I'm working on a store App project and I stuck in the selecting subSection VC because of the UIPickerView not getting the updated value from the array of target!
Here below is my code
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()
}
}
The problem is: title & number Of Rows In Component are not getting the value from (generatedTypes) array after it get updated when cell did selected!
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!")
}
I did many many breakpoints and I'm sure that [generatedTypes] array has a value..but I don't know why it always called inside pickerView before ti get updated!
please help me
Thank you in advance
You may need to reload the picker end of updateTypesarray
func updateTypesarray(){
......
self.pickerView.reloadAllComponents()
}