iosswifttableviewcustom-cell

Getting nil while trying to access all cells data when cells are beyond screen (not visible) in Tableview iOS


I have 6 custom cells in my tableview. And each cells data I need to check whether user filled or not on action of Submit button and I need to pass those information to server if all the cells info filled because all cells are mandatory to fill before to send server. If anything user missed to fill, I must throw alert to fill that cell.

But, I am getting cell is nil if the cell is beyond screen.

 @IBAction func submitBtnAction(_ sender: Any) {
        
        let dictionary = self.fetchSelectedCellsInfo()
//call api here by passing dictionary
}

    func fetchSelectedCellsInfo() -> [[String: String]] {
        
       // self.appSurveyTableview.reloadData()
        if eachIndexInfo.count > 0 {
            for (index, item) in eachIndexInfo.enumerated() {
                
                let cellId = activeCells[index]
                let indexPath = IndexPath.init(row:index, section: 0)

                switch cellId.identifier {
                    
                case .yesNoCell:
                    
                    let yesNoCell = self.appSurveyTableview.cellForRow(at: indexPath) as? YesNoTableViewCell
                        if let savedFlag = yesNoCell?.savedFlagValue, savedFlag.isEmpty == false {
                        selectedArrDictionary.append(["yesNoCell.savedFlagValue":savedFlag])
                    } else {
                        self.showBasicAlert(title: "Please provide feedback for \(item.question ?? "") ", message: "")
                     }
                    
                    break
                    
                case .TextWithCheckBoxesCell:
                    let textWithCheckBoxCell = self.appSurveyTableview.cellForRow(at: indexPath) as? TextWithCheckBoxesTableViewCell
                    if let selectedOption = textWithCheckBoxCell?.selectedOption, selectedOption.isEmpty == false {
                        selectedArrDictionary.append(["savedFlagValue":selectedOption])
                    } else {
                        self.showBasicAlert(title: "Please provide feedback for \(item.question ?? "") ", message: "")
                    }
                    break
                    
                case .textWithRadioBtnsCell:
                    let textWithRadioBtnsCell = self.myTableview.cellForRow(at: indexPath) as? TextWithRadioButtonsTableViewCell
                    if let selectedOption = textWithRadioBtnsCell?.selectedOption, selectedOption.isEmpty == false {
                        selectedArrDictionary.append(["savedFlagValue":selectedOption])
                    } else {
                        self.showBasicAlert(title: "Please provide feedback for \(item.question ?? "") ", message: "")
                    }
                    break
                    
                case .ratingCell:
                    let ratingCell = self.myTableview.cellForRow(at: indexPath) as? RatingTableViewCell
                    if let labelText = ratingCell?.ratingTextLabel.text, labelText.isEmpty == false {
                        selectedArrDictionary.append(["savedFlagValue":labelText])
                    } else {
                        self.showBasicAlert(title: "Please provide feedback for \(item.question ?? "") ", message: "")
                    }
                    break
                    
                case .ratingWithTextBoxCell:
                    let ratingWithTextBoxCell = self.myTableview.cellForRow(at: indexPath) as? RatingWithTextBoxTableViewCell
                    if let ratingTextLabelText =  ratingWithTextBoxCell?.ratingTextLabel.text, let textViewText = ratingWithTextBoxCell?.textView.text, ratingTextLabelText.isEmpty == false && textViewText.isEmpty == false{
                        selectedArrDictionary.append(["savedFlagValue":ratingTextLabelText])
                    } else {
                        self.showBasicAlert(title: "Please provide feedback for \(item.question ?? "") ", message: "")
                    }
                    break
                    
                case .appTextBoxCell:
                    let textWithTextBoxCell = self.myTableview.cellForRow(at: indexPath) as? TextboxTableCell
                    if let textViewText = textWithTextBoxCell?.textView.text, textViewText.isEmpty == false {
                        selectedArrDictionary.append(["savedFlagValue":textViewText])
                    } else {
                        self.showBasicAlert(title: "Please provide feedback for \(item.question ?? "") ", message: "")
                    }
                    break
                    
                case .none:
                    break
                }
            }
        }
        return selectedArrDictionary
    }

How to access all cells info? Any suggestions?


Solution

  • I have fixed the issue by myself by storing cells info into singleton class.

    class Manager {
        public static let shared = Manager()
        
        public var ratingSelectedText = String()
        public var textBoxSelectedText = String()
        public var ratingWithTextboxText = String()
        public var textWithCheckBoxSelectedText = String()
        public var radioButtonsSelectedText = String()
        public var textWithTextBoxText = String()
        public var yesNoSelectedText = String()
        
    }
    

    And storing them in corresponding cells and accessing them in submit button action. Now working fine as expected even beyond cells are getting.

    Thank you all who gave suggestions.