首页 > 解决方案 > 选择单元格时更新 UITableView 部分

问题描述

我正在寻找有关如何执行以下行为的提示/最佳实践。我有一个包含一些部分的 UITableView,每个部分都包含自定义 UITableViewCells。示例:第 1 部分包含一个带有 UILabel 的 UITableViewCell,第 2 部分包含一个包含一些 UITextField 的 UITableViewCell。当我从第 1 节(一个 UILabel)中选择一个 UITableViewCell 时,我得到一个对象。有谁知道如何用第 1 节中所选对象的变量填充 UITextFields?

这是一个示例用例:我有一个 UITableView 包含 2 个部分,在第 1 部分中我有学生姓名列表,在第 2 部分中我有一个包含一些 UITextFields 的自定义 UITableViewCell,当我从第 1 部分中选择一个名称时,我想在 UITextFields 中获取该学生的详细信息。

标签: swiftuitableview

解决方案


您必须使用通知

在 TableViewController 中:
public let StudentSelectedNotification = Notification.Name(rawValue: "StudentSelectedNotification")
didSelectRowAt indexPath
NotificationCenter.default.post(Notification.init(name: StudentSelectedNotification, object: MyObject))
viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(self.fillTextField(_:)), name: StudentSelectedNotification, object: nil)
在 TableViewController 中:
func fillTextField(_ notification: Notification) { let myObject = notification.object // set the text of textfield here }


推荐阅读