从 swift 中的另一个表视图选定行更新表视图行标签

Update tableview row label from another tableview selected row in swift

我遇到了一个问题,我有一个 UITableView,每行都有一个标签和按钮,当从特定行单击按钮时,它将导航到下一个视图并且它有 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.下面是我的代码。 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)
}

您可以使用委托模式:

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

在您的 FirstViewController.swift 中遵守该协议

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

在你的 CountryViewController.swift 中创建一个名为 delegate/anyName 的变量

var delegate: SelectCountry?

在你的 buttonClick 方法中

customCell.delegate = self

在您的 CountryViewController 中的 didSelectRowAt 方法中

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

您的标签将更新为您在 CountryViewController 中选择的国家名称。

注意:此处的名称只是占位符,您可以使用自己的名称 protocol/methods

  1. 第一个控制器:
  • "CountrySelectionDelegate" 在您的第一个控制器中确认此委托

  • 接下来,Pass/Store您的第一个ViewController单元格选择索引。

  • 转到您的国家/地区控制器,Select 国家/地区,通过“func selectedCountry(country: String,index: Int) {}”传递它,更新您的自定义 Data/Array .

  • 最后使用更新的自定义数据重新加载您的 Table 视图。

    class 首先ViewController: UIViewController,UITableViewDelegate,UITable ViewDataSource,国家SelectionDelegate {

          @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. 国家/地区ViewController:

    进口UI套件

     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)
         }
     }