[英]TableView cell not updating after Selecting item using delegate Swift
我正在尝试为选择项实现委托方法。 我在更新 tableView 单元格时感到困惑。 因为我有多个部分,列表将用于不同的单元格。 如何更新我的 tableview 单元格行或模型,以便重新加载 tableView?
模型:
struct Unit : Codable {
let sectionList : [SectionList]?
}
struct SectionList : Codable {
let title : String?
let items : [Item]?
}
struct Item : Codable {
let actionType : Int?
let actionUrl : String?
let bgColor : String?
let booleanValue : Bool?
let textField : String?
let textValue : String?
let unitId : Int?
let latitude : Double?
let longitude : Double?
let actionParamData: String?
let actionTitle: String?
let pickList: [SectionList]?
let multiSelect: Bool?
let selectedValue: [String]?
let version: Int?
let masterId: Int?
let itemValue: String?
}
第一个视图控制器:
var AppData: Unit?
func listFunc(tvc2: ViewController, didSelectList listValue: String) {
print(listValue)
let indexPathRow:Int = 0
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)
}
SecondViewController:(包含列表)
protocol ListDelegate {
func listFunc(tvc2: ViewController, didSelectList listValue: String)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
//print(currentCell.textLabel?.text as Any)
currentCell.accessoryType = .checkmark
delegate?.listFunc(tvc2: self, didSelectList: currentCell.textLabel?.text ?? "")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
self.navigationController?.pushViewController(vc, animated: true)
}
嗨,您对委托的工作方式感到有些困惑。 在这里,我正在修改您的代码。 假设你有两个 viewControllers 即FirstViewController
和SecondViewController
现在你想要的是从SecondViewController
发送回数据或重新加载数据到FirstViewController
。 所以,你需要做的是建立一个Protocol
上SecondViewController
并确认Protocol
中FirstViewController
。 以下是您的代码示例:
第一视图控制器
class FirstViewController: UIViewControlller, ListDelegate {
var selectedIndex: Int?
var selectedSection: Int?
//Click event for navigation from FirstViewController to SecondViewController
@IBAction func BackButtonAction(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
func listFunc(listValue: String) {
AppData?.sectionList?[selectedSection!].items?[selectedIndex!].textField = listValue
tableView.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Add these two line in your code
selectedIndex = indexPath.row
selectedSection = indexPath.section
}
}
第二个视图控制器
protocol ListDelegate {
func listFunc(listValue: String)
}
class SecondViewController: UIViewControlller {
var delegate: ListDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
//print(currentCell.textLabel?.text as Any)
currentCell.accessoryType = .checkmark
delegate?.listFunc(listValue: currentCell.textLabel?.text ?? "")
self.navigationController?.popViewController(animated: true)
}
}
Response.swift
//Change let to var for these
var textField : String?
var textValue : String?
上面的代码可能会解决您的问题。 请试试这个代码。 Happy To Help
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.