【发布时间】: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