【问题标题】:Update tableview row label from another tableview selected row in swift快速从另一个 tableview 选定行更新 tableview 行标签
【发布时间】:2021-10-30 22:47:33
【问题描述】:

我遇到了一个问题,即我有一个UITableView,每行都有一个标签和一个按钮,当从特定行单击该按钮时,它将导航到下一个视图,并且它有一个国家/地区的UITableView 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)
}

【问题讨论】:

  • 使用选定的 countryName 更新您的自定义数据并重新加载您的表格视图。你能解释一下选择后你想在哪里显示国家名称吗?
  • 如果我们从 FirstViewController 中选择第 5 行,它将重定向到 CountryViewController 并且所选行的国家名称应该更新到 FirstViewController 中的第 5 行

标签: ios swift uitableview


【解决方案1】:

您可以使用委托模式:

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 中选择的国家/地区名称进行更新。

注意:这里的名称只是占位符,您可以将自己的名称用于协议/方法

【讨论】:

  • 如何为特定表格行的标签分配。例如,如果我们要更新第 5 行的标签。
  • 如果我们从 FirstViewController 中选择第 5 行,它将重定向到 CountryViewController 并且所选行的国家名称应该更新到 FirstViewController 中的第 5 行
  • @RakshitKumar 看到您在 FirstViewController.swift 中将标签设置为按钮。你只需要在index = sender.tag 等@objc 方法中获取标签。当您的委托函数在那里触发时,您可以获取customData[index].title = countryName。您还需要重新加载该行/完整的 tableView 数据。
  • 已修复。谢谢@GhulamMustafa。对于迟到的回复,我深表歉意。
【解决方案2】:
  1. 第一控制器:
  • “CountrySelectionDelegate”在您的第一个控制器中确认此委托

  • 接下来,传递/存储您的 FirstViewController 单元格选择索引。

  • 转到您的国家控制器,选择国家,通过 "func selectedCountry(country: String,index: Int) {}" 传递它,更新您的自定义数据/数组。

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

    类 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:

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

【讨论】:

    猜你喜欢
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2020-03-14
    相关资源
    最近更新 更多