快速获取tableview单元格值nil

时间:2020-09-08 17:59:48

标签: swift tableview

In this image two sections are showing and third section will be shown after scrolling我有3个部分的表格视图。每个部分都有一行。在我的模拟器屏幕上,仅显示2个部分。如果我滚动表格视图,将显示第三个单元格。有一个保存按钮,通过单击它我正在将数据传递到另一个视图控制器。如果我不滚动表格视图并单击“保存”按钮,则第三部分的单元格为nil。

我的代码是:-

let indexPath = IndexPath(row: 0, section: 0)
        let indexPath1 = IndexPath(row: 0, section: 1)
        let indexPath2 = IndexPath(row: 0, section: 2)
        let cell1 = tableView.cellForRow(at: indexPath) as? PatientInformationTableViewCell
        let cell2 = tableView.cellForRow(at: indexPath1) as? PatientAdressTableViewCell
        let cell3 = tableView.cellForRow(at: indexPath2) as? OtherInfoTableViewCell

 guard let name = nameTextField.text,
            let id = cell1.patientTextField.text,
            let dob = cell1.dobTextField.text,
            let street = cell1.streetAdressTextField.text,
            let city = cell2.cityTextField.text,
            let state = cell2.stateTextField.text,
            let zip = cell2.zipTextField.text,
            let country = cell2.CountryTextField.text,
            let practioner = cell3.practionerTextField.text,
            let lcode = cell3.lcodeTextField.text,
            let qty = cell3.qtyTextField.text,
            let prescription = cell3.PrescriptionTextView.text,
            let klLevel = cell3.kLevelTextField.text,
            let deviceType = cell3.deviceTypeTextField.text,
        let guardian = cell3.guardianTextField.text,
        let relationship = cell3.relationshipwithPatient.text
        
        else {
            return
        }
        
        let patient = PatientInformationModel(name: name, id: id, dob: dob, streetAdress: street, city: city, state: state, zip: zip, country: country, klevel: klLevel, deviceType: deviceType, visitType: "visitType", deliveryLocation: "deliveryLocation", lCode: lcode, qty: qty, descripePrescription: prescription, practioner: practioner, guardian: guardian, relationship: relationship)
       
        
        let encoder = JSONEncoder()
        if let encoded = try? encoder.encode(patient) {
            let defaults = UserDefaults.standard
            defaults.set(encoded, forKey: "SavedPerson")
        }
        
        
        if let storyBoard = UIStoryboard(name: "Pdf", bundle: nil) as? UIStoryboard{
            captureSignature { (sign) in
                signatureImage = sign?.image
                print("signature image is \(signatureImage)")
                
            }
             let PatientListViewController = storyBoard.instantiateViewController(identifier: "GeneratePdfViewController") as? GeneratePdfViewController

            PatientListViewController?.signatureImage = signatureImage
                              self.navigationController?.pushViewController(PatientListViewController!, animated: true)
            }

在上面的代码中,如果我单击保存按钮而不滚动到底部,我将得到cell3值nil。是否有任何结果。请帮助我。

0 个答案:

没有答案