Search code examples
iosswiftuitableviewcell

Is there a way to get the value from a text filed in a table view cell to use it in another table view cell?


I'm building an iOS e-commerce app which sells shoe products. This question is about the Checkout screen which is a UITableViewController to collect billing information of the user and save it in Firebase.

Below UITableViewController contains two custom UITableView cells. The first cell contains some text fields to get billing information(Email, Card Number, Expiration Date and CVC) from the user, while the second cell below that contains a submit button to save them in Firebase.

My requirement is to get the billing information from the user and save it in Firebase when clicking the Submit button. (Text fields and submit button are in two separate UITableView cells)

Could you please help me with this. Please find below code snippets.

CheckoutViewController.swift

class CheckoutTableViewController: UITableViewController {

    // MARK: - Properties

    var shoes : [Shoe]! {
        didSet {
            tableView.reloadData()
        }
    }

    // MARK: - Structs

    struct Storyboard {
        static let billingInfoCell = "billingInfoCell"
        static let submitButtonCell = "submitButtonCell"
    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - Data source

     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.billingInfoCell, for: indexPath) as! BillingInfoTableViewCell // Contains billing information text fields
            return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.submitButtonCell, for: indexPath) as! SubmitButtonTableViewCell // Contains Submit button
            return cell
        } else {
            return UITableViewCell()
        }
    }

}

BillingInfoTableViewCell.swift

class BillingInfoTableViewCell: UITableViewCell {

    // MARK: - IBOutlets

    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var cardNumberTextField: UITextField!
    @IBOutlet weak var expirationDataTextField: UITextField!
    @IBOutlet weak var securityNumberTextField: UITextField!


}

SubmitButtonTableViewCell.swift

class SubmitButtonTableViewCell: UITableViewCell {

    // MARK: - IBActions
    @IBAction func submitOrderButtonTapped(_ sender: UIButton) {
        print("Submit button tapped!")
    }

}

Solution

  • Please follow the below steps.

    Make the outlet of UITableView

    @IBOutlet weak var tableView: UITableView!
    

    Make static UITableviewCell

    lazy var cellBillingInfo = tableView.dequeueReusableCell(withIdentifier: Storyboard.billingInfoCell) as! BillingInfoTableViewCell
    lazy var cellSubmit = tableView.dequeueReusableCell(withIdentifier: Storyboard.submitButtonCell) as! SubmitButtonTableViewCell
    

    Add target for submit button as below.

    cellSubmit.btnSumit.addTarget(self, action: #selector(submitOrderButtonTapped(_:).tou), for: .touchUpInside)
    

    Define submit button method

    @objc func submitOrderButtonTapped(_ sender: UIButton) {
            print(cellBillingInfo.emailTextField.text)
            print(cellBillingInfo.cardNumberTextField.text)
            print(cellBillingInfo.expirationDataTextField.text)
            print(cellBillingInfo.securityNumberTextField.text)
    }
    

    Final Code:

    class ViewController: UIViewController {
        
        @IBOutlet weak var tableView: UITableView!
        // MARK: - Properties
        
        lazy var cellBillingInfo = tableView.dequeueReusableCell(withIdentifier: Storyboard.billingInfoCell) as! BillingInfoTableViewCell
        lazy var cellSubmit = tableView.dequeueReusableCell(withIdentifier: Storyboard.submitButtonCell) as! SubmitButtonTableViewCell
        
        var shoes : [Shoe]! {
            didSet {
                tableView.reloadData()
            }
        }
        
        // MARK: - Structs
        
        struct Storyboard {
            static let billingInfoCell = "billingInfoCell"
            static let submitButtonCell = "submitButtonCell"
        }
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        
        // MARK: - Data source
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 2
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            if indexPath.row == 0 {
                return cellBillingInfo
            } else if indexPath.row == 1 {
                cellSubmit.btnSumit.addTarget(self, action: #selector(submitOrderButtonTapped(_:).tou), for: .touchUpInside)
                return cellSubmit
            } else {
                return UITableViewCell()
            }
        }
        
        @objc func submitOrderButtonTapped(_ sender: UIButton) {
            print(cellBillingInfo.emailTextField.text)
            print(cellBillingInfo.cardNumberTextField.text)
            print(cellBillingInfo.expirationDataTextField.text)
            print(cellBillingInfo.securityNumberTextField.text)
        }
    }
    
    class SubmitButtonTableViewCell: UITableViewCell {
        @IBOutlet weak var btnSumit: UIButton!
    }