Search code examples
iosuitableviewtableviewcellswift4

how to get textfields texts in tableview cells that this tableview is in another table view controller in swift 4?


I have a table view controller that has a table view in one of the cells and in inner tableview I have sample cell that has textfield I want when user starts editing textfield when keyboard is dismissed all of the textfields text storage in an array heres is the dismiss codes

@objc func dismissKeyboard() {

    view.endEditing(true)

    let index = IndexPath(row: 0, section: 6)
    let cell = tableView.cellForRow(at: index)

    if (cell as? editCertificatesCell) != nil {
    for i in 0..<certificates.count {

        let index = IndexPath(row: i, section: 0)
        let cell = tableView.cellForRow(at: index)

        if let cell = cell as? eCCell {

            let b = cell.ceText.text!
            print(b)

            // here is your field
        }
    }
    }

and here is the table view cell class that has tableview in it

import UIKit

class editCertificatesCell: UITableViewCell {

@IBOutlet weak var addCertificate: UIButton!

@IBOutlet weak var certificatesListTableView: UITableView!

override func awakeFromNib() {
    super.awakeFromNib()

    // Initialization code

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

@IBAction func ceD(_ sender: UIButton) {
  }

 }


  extension editCertificatesCell {

func setCollectionViewDataSourceDelegate
    <T: UITableViewDelegate & UITableViewDataSource>
    (_ dataSourceDelegate:T , forRow row : Int )

{

    certificatesListTableView.delegate = dataSourceDelegate
    certificatesListTableView.dataSource = dataSourceDelegate
    certificatesListTableView.reloadData()
}




 }

and here is the main table view indexPath codes that has table view in it

let eCCells = tableView.dequeueReusableCell(withIdentifier: "eCCell", for: indexPath) as! eCCell

        print(certificates[indexPath.row])
        eCCells.ceText.text = certificates[indexPath.row]
        eCCells.ceDelete.tag = indexPath.row
        eCCells.ceDelete.addTarget(self, action: #selector(EditProfileTableViewController.deleteCer), for: UIControlEvents.touchUpInside)

and here is the error that I received in the console But without crash

invalid capability (0x14) "Unable to insert COPY_SEND

simply I just want when user edited the textfields make an array of textfields texts and then reload inner tableview and show them


Solution

  • here is the answer I should use this instead of dismissKeyboard() function

     @objc func dismissKeyboard() {
    
        view.endEditing(true)
    
        let index = IndexPath(row: 0, section: 6)
        let cell = tableView.cellForRow(at: index) as! editCertificatesCell
    
        var certificatesSample = [String]()
        certificatesSample.removeAll()
        for i in 0..<certificates.count {
    
            let index1 = IndexPath(row: i, section: 0)
            cell.certificatesListTableView.cellForRow(at: index1)
            let cell = cell.certificatesListTableView.cellForRow(at: index1)
    
            if let cell = cell as? eCCell {
    
                let b = cell.ceText.text!
                print(b)
                certificatesSample.append(b)
            }
        }
    
        certificates.removeAll()
        certificates = certificatesSample
        certificatesSample.removeAll()
    
    
    }