【发布时间】:2019-11-29 07:38:14
【问题描述】:
我对此很陌生。我遇到了选择项目的问题,并在特定索引单元格(重新加载)上回发到 tableview。 场景:我有两个视图控制器,并且都包含 tableview。
FirstViewController: 我已经将来自 web 服务的数据显示到 Tableview 中,但是在不同的操作类型上,我使用了 didSelectRow 函数,该函数显示给ListViewController 用于项目选择。在 FirstViewController 中,主要模型是 Unit。
ListViewController: tableView 中显示的简单数据列表供选择。
问题: 是如何从 ListViewController 中选择项目并将数据传递给 FirstViewController 表格视图,在特定单元格上发生更改,然后我必须发布具有更新值的值。如何重新加载 tableview 或模型?
型号:
struct Unit : Codable {
let sectionList : [SectionList]?
}
struct SectionList : Codable {
let title : String?
let items : [Item]?
}
struct Item : Codable {
let actionType : Int?
let textField : String?
let textValue : String?
let pickList: [SectionList]?
let multiSelect: Bool?
let selectedValue: [String]?
let version: Int?
let masterId: Int?
let itemValue: String?
}
更新了 FirstController:
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
}
}
更新:ListViewController:
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)
}
}
【问题讨论】:
-
@dahiya_boy 我已经阅读了代表,但不知道如何选择发送到上一个 viewController tableView 单元格的项目..
-
您需要在委托函数中传递选定的数据。有关更多详细信息,请访问此appcoda.com/swift-delegate。如果您在直接进入主项目之前尝试一些委托和协议的演示,那就更好了。这并不难,但您需要先清除基础知识。
-
@dahiya_boy 我正在努力实现但达不到标准。我可以和你分享我的测试项目吗?
-
好的,分享 github 链接。
标签: ios swift uitableview model didselectrowatindexpath