2

I'm new to Swift. I have two table view controllers, one with static cells and one with dynamic ones. I basically want to let a user select his marital status on the second table view controller and send his choice back to the first table view controller (and display his selection on the cell "Marital Status"). Here is a screenshot of my storyboard:

Storyboard

Current code on second table view controller:

import UIKit

class SecondTableViewController: UITableViewController {
     let maritalStatusArray: [String] = ["Single", "Married"]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  maritalStatusArray.count

    }


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

        let cell = tableView.dequeueReusableCell(withIdentifier: "MaritalStatusCell", for: indexPath)
        cell.textLabel?.text = maritalStatusArray[indexPath.row]
        return cell

    }
}

My guess is that I have to add a segue from the dynamic cell of the second view controller back to the first one. Is that right ?

Supposing this is the way to do it, I have afterwards to update the text of the static label to include the choice made by the user. Any ideas ?

fbelfort
  • 283
  • 1
  • 4
  • 12

2 Answers2

1

There few ways by which you can implement the callback functionality to pass data.

  1. Delegate
  2. Using Block CallBack
  3. Post Notification

But I would suggest to use delegate which is best way, Post Notification is also a way but I do not want to prefer.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
1

You can use custom delegate to do this:

ViewController.swift:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SecondViewControllerProtocol {

    @IBOutlet weak var userInfoTableView: UITableView!

    var userInfoArray: [String] = []


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        userInfoArray = ["Marital Status","Canton","Commune","Religion"]
       self.userInfoTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let userInfoCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        userInfoCell.textLabel?.text = userInfoArray[indexPath.row]
        if indexPath.row == 0{
            userInfoCell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        }
        return userInfoCell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0{
            let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewControllerIdentifier") as! SecondViewController
            secondVC.statusDelegate = self
            self.navigationController?.present(secondVC, animated: true, completion: nil)
        }
    }

    func changeMaritalStatus(type: String){
        let  maritalStatusCell = userInfoTableView.cellForRow(at: IndexPath(row:0 , section:0))
        maritalStatusCell?.textLabel?.text = String("Marital Status: \(type)")
    }
}

SecondViewController.swift:

import UIKit

protocol SecondViewControllerProtocol {
    func changeMaritalStatus(type: String)
}

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var maritalStatusTableView: UITableView!

    var maritalStatusArray: [String] = []

    var statusDelegate  : SecondViewControllerProtocol? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        maritalStatusArray = ["Single","Married","Divorced"]
        self.maritalStatusTableView.register(UITableViewCell.self, forCellReuseIdentifier: "maritalStatuscell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let maritalStatusInfoCell = tableView.dequeueReusableCell(withIdentifier: "maritalStatuscell", for: indexPath)
        let infoLabel: UILabel = UILabel.init(frame: CGRect(x: 10, y: 5, width: 250, height: 50))
        infoLabel.text = maritalStatusArray[indexPath.row]
        maritalStatusInfoCell.addSubview(infoLabel)
        return maritalStatusInfoCell
    }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        statusDelegate?.changeMaritalStatus(type: maritalStatusArray[indexPath.row])
        self.dismiss(animated: true, completion: nil)
    }

}

GitHub link:

https://github.com/k-sathireddy/CustomDelegatesSwift

Output:-

enter image description here

KSR
  • 1,699
  • 15
  • 22