我遇到了一個問題,我有一個UITableView,每一行都有一個標簽和按鈕,當點擊某一行的按鈕時,它將導航到下一個視圖,它有一個UITableView的國家串列,當選擇國家時,它將彈出到前一個視圖,我想更新所選行的國家名稱,誰能指導我如何更新它。下面是我的代碼。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) 作為! 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)
}
uj5u.com熱心網友回復:
你可以使用委托模式:
protocol SelectCountry {
func countrySelected(withName countryName: String)
}
在你的FirstViewController.swift中符合該協議的要求
extension FirstViewController。SelectCountry {
func countrySelected(withName countryName: String) {
//為你的標簽指定國家名稱。
在你的CountryViewController.swift中制作一個變數,叫做delegate/anyName,你想怎么做就怎么做
var delegate: SelectCountry?
在你的buttonClick方法中
customCell.delegate = self
在你的CountryViewController的didSelectRowAt方法中
。delegate?.countrySelected(withName: CountryList[indexPath.row] )
你的標簽將被更新為你在CountryViewController中選擇的國家名稱。
注意:這里的名字只是占位符,你可以使用你自己的協議/方法的名字
。uj5u.com熱心網友回復:
- 第一個控制器:
"CountrySelectionDelegate "在你的第一個控制器中確認這個委托
接下來,傳遞/存盤你的FirstViewController單元格選擇索引。
進入你的國家控制器,選擇國家,通過 "func selectedCountry(country: String,index: Int) {}"傳遞它。更新你的自定義資料/陣列。
最后,用更新的自定義資料重新加載你的表視圖。
class FirstViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, CountrySelectionDelegate {
@IBOutlet 弱 var yourFirstTable: UITableView! var customData = [Details(title: "Title-1", country: ""),Details(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 = "標題。" custInfo.title cell.yourCountryLabel.text = (custInfo.country.count > 0 ? "國家。(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! /span>, animated: true) } }
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 = ["印度"/span>,"美國"/span>,"英國"/span>, "尼泊爾","孟加拉國","巴基斯坦", "不丹"] 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) } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311707.html
標籤:
