dropdown array becomes empty for few cells after adding cells in tableview and scroll up

Viewed 34

Initially when I tap of hierarchy button and parentLocationLevelTwoBtnAction at that time parentLocationNameArr is not empty but after adding few more cells and I scroll up parentLocationNameArr is empty

import UIKit
import DropDown
import ObjectMapper

//Stock Btn Cell Delegate
protocol AddStorageLocationMasterBtnCellDelegate : AnyObject {
    func didPressButton(_ tag: Int)
    func didPressHierachyButton(_ tag: Int)
    func didPressHierachytext(_ tag: Int,sender:String)
    func didPressParenttext(_ tag: Int,sender:String, id:String)
    func didPressParentTwotext(_ tag: Int,sender:String, id:String)
}

class AddStorageLocationMasterTVCell: UITableViewCell {
    
    @IBOutlet var parentLTextLHeight: NSLayoutConstraint!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var storageLocationNameView: UIView!
    @IBOutlet var warnDescriptionbtn: UIButton!
    @IBOutlet weak var storageLocationNameTF: UITextField!
    @IBOutlet var warnNamebtn: UIButton!
    @IBOutlet weak var descriptionView: UIView!
    @IBOutlet weak var descriptionTextView: UITextView!
    @IBOutlet weak var hierachyView: UIView!
    @IBOutlet weak var hierachyTF: UITextField!
    @IBOutlet weak var hierachyBtn: UIButton!
    @IBOutlet weak var parentLocationView: UIView!
    @IBOutlet weak var parentLocationTF: UITextField!
    @IBOutlet weak var parentBgView: UIView!
    @IBOutlet weak var parentHeightConstant: NSLayoutConstraint!
    
    @IBOutlet weak var parentLocationLevelOneBtn: UIButton!
    @IBOutlet weak var parentLocationLevelTwoView: UIView!
    
    @IBOutlet weak var parentLocationLevelTwoTF: UITextField!
    @IBOutlet weak var parentLocationTextLabel: UILabel!
    
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var stackViewHeight: NSLayoutConstraint!
    @IBOutlet weak var parentLcationLabel: UILabel!
    
    var cellDelegate: AddStorageLocationMasterBtnCellDelegate?
    
    let dropDown = DropDown() //2
    var selectedId = 0
    
    var accountID = ""
    var storageLocationArr = NSMutableArray()
    var storageLocationLevelThreeArr = NSMutableArray()
    var serviceVC = ServiceController()
    var parentLocationDict = [String]()
    var parentLocationId = [String]()
    var parentLocationArr = [NSArray]()
    
    var parentLocationNameArr = [String]()
    var parentLocationNameIdsArr = [String]()
    var parentLocationDetailsDict = NSMutableArray()
    
    var parentLocationNameLevethreeArr = [String]()
    var parentLocationNameLevethreeIdsArr = [String]()
    var parentLocationDetailsLevethreeDict = NSMutableArray()
    
    var levelValue = 0
    var idLevelThree = ""
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        self.parentBgView.isHidden = true
        self.parentHeightConstant.constant = 0
        
        storageLocationNameView.layer.borderColor = UIColor.gray.cgColor
        storageLocationNameView.layer.borderWidth = 0.5
        storageLocationNameView.layer.cornerRadius = 3
        storageLocationNameView.clipsToBounds = true
        
        descriptionView.layer.borderColor = UIColor.gray.cgColor
        descriptionView.layer.borderWidth = 0.5
        descriptionView.layer.cornerRadius = 3
        descriptionView.clipsToBounds = true
        
        hierachyView.layer.borderColor = UIColor.gray.cgColor
        hierachyView.layer.borderWidth = 0.5
        hierachyView.layer.cornerRadius = 3
        hierachyView.clipsToBounds = true
        
        parentLocationView.layer.borderColor = UIColor.gray.cgColor
        parentLocationView.layer.borderWidth = 0.5
        parentLocationView.layer.cornerRadius = 3
        parentLocationView.clipsToBounds = true
        
        parentLocationLevelTwoView.layer.borderColor = UIColor.gray.cgColor
        parentLocationLevelTwoView.layer.borderWidth = 0.5
        parentLocationLevelTwoView.layer.cornerRadius = 3
        parentLocationLevelTwoView.clipsToBounds = true
        
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 5.0
        bgView.layer.shadowColor =  #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        bgView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        bgView.layer.shadowRadius = 6.0
        bgView.layer.shadowOpacity = 0.7
        
        hierachyTF.text = "Level 1"
        selectedId = 1
    }
    
    @IBOutlet var parentButton: UIButton!
    //    override func setSelected(_ selected: Bool, animated: Bool) {
    //        super.setSelected(selected, animated: animated)
    //
    //        // Configure the view for the selected state
    //    }
    @IBAction func deleteBtnAction(_ sender: UIButton) {
        
        cellDelegate?.didPressButton(sender.tag)
    }
    var tagVal=Int()
    
    @IBAction func hierachyBtnAction(_ sender: UIButton) {
        
        let selectedTagHierachy = sender.tag
        
        cellDelegate?.didPressHierachyButton(selectedTagHierachy)
        dropDown.dataSource = ["Level 1","Level 2","Level 3"]//4
        dropDown.anchorView = sender //5
        dropDown.bottomOffset = CGPoint(x: 0, y: sender.frame.size.height) //6
        dropDown.show() //7
        
        dropDown.selectionAction = { (index: Int, item: String) in //8
            //              guard let _ = self else { return }
            //              sender.setTitle(item, for: .normal) //9
            self.selectedId = index
            self.hierachyTF.text = item
            self.cellDelegate?.didPressHierachytext(selectedTagHierachy, sender: item)
            print(self.levelValue)
            if index == 0 {
                
                self.parentBgView.isHidden = true
                self.parentHeightConstant.constant = 0
            }
            else if index == 1{
                self.levelValue = index+1
                self.get_StorageLocationMaster_API_Call(level: index+1, tag: selectedTagHierachy)
                self.parentBgView.isHidden = false
                self.parentHeightConstant.constant = 120
                self.parentLcationLabel.text = "Parent Location L1"
                self.parentLocationLevelTwoView.isHidden = true
            }
            else if index == 2{
                self.levelValue = index+1
                self.get_StorageLocationMaster_API_Call(level: index+1, tag: selectedTagHierachy)
                self.parentBgView.isHidden = false
                self.parentHeightConstant.constant = 120
                self.parentLcationLabel.text = "Parent Location L2 and L1"
                self.parentLocationLevelTwoView.isHidden = false
            }
        }
    }
    
    @IBAction func parentLocationBtnAction(_ sender: UIButton) {
        
        let selectedTagParentLocation = sender.tag
        //        cellDelegate?.didPressParenttext(<#T##tag: Int##Int#>, sender: sender, id: <#T##String#>)
        dropDown.dataSource = parentLocationNameLevethreeArr//4
        dropDown.anchorView = sender //5
        dropDown.bottomOffset = CGPoint(x: 0, y: sender.frame.size.height) //6
        dropDown.show() //7
        dropDown.selectionAction = {  (index: Int, item: String) in //8
            //              guard let _ = self else { return }
            //              sender.setTitle(item, for: .normal) //9
            self.parentLocationLevelTwoTF.text = item
            self.cellDelegate?.didPressParentTwotext(selectedTagParentLocation, sender: item, id:self.parentLocationNameLevethreeIdsArr[index] )
        }
        
        
    }
    
    @IBAction func parentLocationLevelTwoBtnAction(_ sender: UIButton)
    {
        
        let selectedTagParentLocation = sender.tag
        
        dropDown.dataSource = parentLocationNameArr//4
        dropDown.anchorView = sender //5
        dropDown.bottomOffset = CGPoint(x: 0, y: sender.frame.size.height) //6
        dropDown.show() //7
        dropDown.selectionAction = {  (index: Int, item: String) in //8
            //              guard let _ = self else { return }
            //              sender.setTitle(item, for: .normal) //9
            //                if self?.parentLocationNameLevethreeArr.count ?? 0>0
            //                {
            //                self?.parentLocationLevelTwoTF.text = self?.parentLocationNameLevethreeArr[0]
            //                    self?.cellDelegate?.didPressParentTwotext(sender.tag, sender: self?.parentLocationNameLevethreeArr[0] ?? "", id: self?.parentLocationNameLevethreeIdsArr[0] ?? "")
            //                }
            self.parentLocationTF.text = item
            self.idLevelThree = self.parentLocationNameIdsArr[index]
            self.cellDelegate?.didPressParenttext(selectedTagParentLocation, sender: item, id: self.parentLocationNameIdsArr[index] )
            print(self.levelValue)
            if self.levelValue==3
            {
                self.get_StorageLocationByParentLocation_API_Call(level: 2, tag: selectedTagParentLocation)
            }
            
        }
    }
    
    // MARK: Get Storage Location Master API Call
    func get_StorageLocationByParentLocation_API_Call(level:Int,tag:Int) {
        
        let defaults = UserDefaults.standard
        accountID = (defaults.string(forKey: "accountId") ?? "")
        //      let newString = parentLocation.replacingOccurrences(of: " ", with: "%20")
        if idLevelThree != ""
        {
            let urlStr = Constants.BaseUrl + getAllStorageLocationByParentUrl + "\(2)/" + "\(idLevelThree)/" + accountID
            
            serviceVC.requestGETURL(strURL: urlStr, success: {(result) in
                
                let respVo:GetStorageLocationMasterRespVo = Mapper().map(JSON: result as! [String : Any])!
                
                DispatchQueue.main.async { [self] in
                    
                    let status = respVo.STATUS_CODE
                    let message = respVo.STATUS_MSG
                    
                    if status == 200 {
                        if message == "SUCCESS" {
                            if respVo.result != nil {
                                if respVo.result!.count > 0 {
                                    let resultObj:[String:Any]=result as? [String:Any] ?? [String:Any]()
                                    let resultArr:NSMutableArray=resultObj["result"]as? NSMutableArray ?? NSMutableArray()
                                    self.storageLocationLevelThreeArr = NSMutableArray()
                                    self.storageLocationLevelThreeArr = resultArr
                                    
                                    self.parentLocationNameLevethreeArr.removeAll()
                                    self.parentLocationNameLevethreeIdsArr.removeAll()
                                    
                                    for obj in self.storageLocationLevelThreeArr {
                                        
                                        let sDict=obj as? NSDictionary
                                        
                                        let isCanEd:Bool = sDict?.value(forKey: "canEdit") as? Bool ?? false
                                        //                                    sDict(sDict?[i] as AnyObject).value(forKey: "canEdit") as? Bool
                                        
                                        if isCanEd == true {
                                            let slocName = sDict?.value(forKey: "slocName") as? String ?? ""
                                            let idsStr = sDict?.value(forKey: "_id") as? String ?? ""
                                            self.parentLocationNameLevethreeIdsArr.append(idsStr)
                                            self.parentLocationNameLevethreeArr.append(slocName)
                                            print("level 2 : \(parentLocationNameLevethreeArr)")
                                        }
                                    }
                                    
                                    if self.parentLocationNameLevethreeArr.count > 0 {
                                        self.parentLocationLevelTwoTF.text = self.parentLocationNameLevethreeArr[0]
                                        self.cellDelegate?.didPressParentTwotext(tag, sender: self.parentLocationNameLevethreeArr[0], id: self.parentLocationNameLevethreeIdsArr[0])
                                    }
                                    //                            else{
                                    //                                self.parentLocationLevelTwoTF.text = ""
                                    //                            }
                                    self.parentLTextLHeight.constant=0
                                    self.parentLocationTextLabel.isHidden=true
                                    self.parentLocationTextLabel.text=""
                                }
                                else {
                                    self.parentLTextLHeight.constant=40
                                    self.parentLocationTextLabel.isHidden=false
                                    self.parentLocationTextLabel.text="No parent storage location is created currently. Pls create a parent storage location before creating the storage location when use creates a storage location without a custom parent storage location"
                                    self.parentLocationTextLabel.numberOfLines = 0
                                    self.parentLocationTextLabel.lineBreakMode = .byWordWrapping
                                    self.parentLocationLevelTwoTF.text = ""
                                    self.parentLocationNameLevethreeArr=[String]()
                                    self.parentLocationNameLevethreeIdsArr=[String]()
                                }
                            }
                        }
                    }
                    else {
                        //                      self.view.makeToast(message)
                    }
                }
            }) { (error) in
                //            self.view.makeToast("app.SomethingWentToWrongAlert".localize())
                print("Oops, your connection seems off... Please try again later")
            }
        }
        else
        {
            self.parentLTextLHeight.constant=40
            self.parentLocationTextLabel.isHidden=false
            self.parentLocationTextLabel.text="No level 2 storage location exists currently. Pls create a level 2 storage location before creating levele 3 storage location"
        }
    }
    
    // MARK: Get AddressBook API Call
    func get_StorageLocationMaster_API_Call(level:Int,tag:Int) {
        
        let defaults = UserDefaults.standard
        accountID = (defaults.string(forKey: "accountId") ?? "")
        
        let urlStr = Constants.BaseUrl + getAllStorageLocationByHierachyLevelUrl + "\(1)/" + accountID
        
        serviceVC.requestGETURL(strURL: urlStr, success: {(result) in
            
            let respVo:GetStorageLocationMasterRespVo = Mapper().map(JSON: result as! [String : Any])!
            
            DispatchQueue.main.async {
                
                let status = respVo.STATUS_CODE
                let message = respVo.STATUS_MSG
                
                if status == 200 {
                    if message == "SUCCESS" {
                        if respVo.result != nil {
                            if respVo.result!.count > 0 {
                                let resultObj:[String:Any]=result as? [String:Any] ?? [String:Any]()
                                let resultArr:NSMutableArray=resultObj["result"]as? NSMutableArray ?? NSMutableArray()
                                print(resultArr)
                                self.storageLocationArr = NSMutableArray()
                                self.storageLocationArr = resultArr
                                
                                if  self.storageLocationArr.count==0
                                {
                                    self.parentLTextLHeight.constant=40
                                    self.parentLocationTextLabel.isHidden=false
                                }
                                else
                                {
                                    self.parentLTextLHeight.constant=0
                                    self.parentLocationTextLabel.isHidden=true
                                    let ddict=self.storageLocationArr[0] as? NSDictionary
                                    
                                    self.parentLocationNameArr = [String]()
                                    self.parentLocationNameIdsArr = [String]()
                                    
                                    for obj in self.storageLocationArr {
                                        
                                        let sDict=obj as? NSDictionary
                                        
                                        let isCanEd:Bool = sDict?.value(forKey: "canEdit") as? Bool ?? false
                                        //                                    sDict(sDict?[i] as AnyObject).value(forKey: "canEdit") as? Bool
                                        
                                        if isCanEd == true {
                                            let slocName = sDict?.value(forKey: "slocName") as? String ?? ""
                                            let idsStr = sDict?.value(forKey: "_id") as? String ?? ""
                                            self.parentLocationNameIdsArr.append(idsStr)
                                            self.parentLocationNameArr.append(slocName)
                                            print(self.parentLocationNameArr)
                                            self.parentLocationDetailsDict.addObjects(from: sDict?.value(forKey: "parentLocationDetails") as? NSMutableArray as! [Any])
                                        }
                                    }
                                    let parentLocationDetails=ddict?.value(forKey: "parentLocationDetails")as? NSArray
                                    print(self.levelValue)
                                    if self.levelValue==3
                                    {
                                        if self.parentLocationNameArr.count>0
                                        {
                                            self.idLevelThree = self.parentLocationNameIdsArr[0] ?? ""
                                            self.parentLocationTF.text = self.parentLocationNameArr[0]
                                            self.cellDelegate?.didPressParenttext(tag, sender: self.parentLocationNameArr[0], id: self.parentLocationNameIdsArr[0])
                                        }
                                        else{
                                            self.parentLTextLHeight.constant=40
                                            self.parentLocationTextLabel.isHidden=false
                                            self.parentLocationTextLabel.text="No level 2 storage location exists currently. Pls create a level 2 storage location before creating levele 3 storage location"
                                        }
                                        //                                    if self.parentLocationDetailsDict.count > 0
                                        //                                    {
                                        self.get_StorageLocationByParentLocation_API_Call(level: 2, tag:tag)
                                        print("parent level 2 :\(self.parentLocationNameLevethreeArr)")
                                        //                                    }
                                        
                                    }
                                    else
                                    {
                                        if self.parentLocationNameArr.count>0
                                        {
                                            self.parentLocationTF.text = self.parentLocationNameArr[0]
                                            self.cellDelegate?.didPressParenttext(tag, sender: self.parentLocationNameArr[0], id: self.parentLocationNameIdsArr[0])
                                        }
                                        else{
                                            self.parentLTextLHeight.constant=40
                                            self.parentLocationTextLabel.isHidden=false
                                            self.parentLocationTextLabel.text="No level 1 storage location exists currently. Pls create a level 1 storage location before creating levele 3 storage location"
                                        }
                                    }
                                    
                                }
                            }
                            else {
                                
                            }
                        }
                    }
                }
                else {
                    //                    self.view.makeToast(message)
                }
            }
        }) { (error) in
            //            self.view.makeToast("app.SomethingWentToWrongAlert".localize())
            print("Oops, your connection seems off... Please try again later")
        }
    }
}

Thanks in advance

0 Answers
Related