Home > OS >  Update tableview row label from another tableview selected row in swift
Update tableview row label from another tableview selected row in swift

Time:09-17

I have stuck up with an issue where I have a UITableView with a label and button in each row when the button is clicked from a particular row it will navigate to the next view and it has UITableView with a country list, when selected the country it will popup to the previous view and I want to update the country name with selected row, Could someone guide me how to update it. Below is my code. TIA

FirstViewController.swift

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableCell
            
    let dict = customData[indexPath.row]  as? NSObject
    cell.lblTitle.text = "Title"
    // cell.lblSubTitle.text = ""
    
    cell.selectedButton.tag = indexPath.row
    cell.selectedButton.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
    
    return cell
}
    
@objc func buttonClick(sender: UIButton){
    let customCell = CountryViewController(nibName: nil, bundle: nil)
    self.navigationController?.pushViewController(customCell, animated: true)  
}

CountryViewController.swift

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell", for: indexPath) as! CountryTableCell
    
    cell.lblTitle.text = CountryList[indexPath.row]
    
    return cell
}
    
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let selectedCountry = CountryList[indexPath.row]
     self.navigationController?.popViewController(animated: true)
}

CodePudding user response:

You can use delegation pattern:

protocol SelectCountry {
    func countrySelected(withName countryName: String)
}

in your FirstViewController.swift conform to that protocol

extension FirstViewController: SelectCountry {
func countrySelected(withName countryName: String) {
// Assign country name to your label here
}

in your CountryViewController.swift make a variable called delegate/anyName you want

var delegate: SelectCountry?

in your buttonClick method

customCell.delegate = self

in your CountryViewController in didSelectRowAt method

delegate?.countrySelected(withName: CountryList[indexPath.row])

your label will be updated with country name you selected in CountryViewController.

NOTE: Names are just placeholders here you can use your own names for protocol/methods

CodePudding user response:

  1. First Controller:
  • "CountrySelectionDelegate" confirm this delegate in your first controller

  • Next, Pass/Store your FirstViewController cell selection index.

  • Go to your Country Controller, Select country, Pass it through "func selectedCountry(country: String,index: Int) {}" , Update your custom Data/Array.

  • Lastly reload your Table view with updated Custom data.

    class FirstViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CountrySelectionDelegate {

          @IBOutlet weak var yourFirstTable: UITableView!
          var customData = [Details(title: "Title-1", country: ""),Details(title: "Title - 2", country: ""),]
    
          override func viewDidLoad() {
              super.viewDidLoad()
          }
    
          func selectedCountry(country: String,index: Int) {
              self.customData[index].country = country
              yourFirstTable.reloadData()
          }
    
          func numberOfSections(in tableView: UITableView) -> Int {
              return 1
          }
    
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return customData.count
          }
    
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DetailTableCell
              let custInfo = customData[indexPath.row]
              cell.yourTitleLabel.text = "Title: "   custInfo.title
              cell.yourCountryLabel.text = (custInfo.country.count > 0 ? "Country: \(custInfo.country)" : "Country: ---")
              return cell
          }
    
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
              let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "CountryViewController") as? CountryViewController
              nextVC?.selectedIndex = indexPath.row
              nextVC?.delegate = self
              self.navigationController?.pushViewController(nextVC!, animated: true)
          }
      }
    
  1. CountryViewController:

    import UIKit

     protocol CountrySelectionDelegate {
         func selectedCountry(country: String, index:Int)
     }
    
     class CountryViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
         @IBOutlet weak var countryTable: UITableView!
    
         var selectedIndex: Int = 0
         let countryList = ["India","USA","UK","Nepal","Bangladesh","Pakistan","Bhutan"]
         weak var delegate: CountrySelectionDelegate?
    
         override func viewDidLoad() {
             super.viewDidLoad()
    
         }
    
         func numberOfSections(in tableView: UITableView) -> Int {
             return 1
         }
    
         func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
             return countryList.count
    
         }
    
         func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CountryTableViewCell
             cell.countryLabel.text = countryList[indexPath.row]
             return cell
         }
    
         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
             delegate?.selectedCountry(country: countryList[indexPath.row], index: selectedIndex)
             self.navigationController?.popViewController(animated: true)
         }
     }
    
  • Related