我正在尝试为选择项实现委托方法。我在更新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?
}
FirstViewController:
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)
}
FirstViewController
class FirstViewController: UIViewControlller, ListDelegate { //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) { tableView.reloadData() } }
SecondViewController
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) } }
以上代码可能可以解决您的问题。请尝试此代码。
Happy To Help