swift - 选择单元格时更新 UITableView 部分
问题描述
我正在寻找有关如何执行以下行为的提示/最佳实践。我有一个包含一些部分的 UITableView,每个部分都包含自定义 UITableViewCells。示例:第 1 部分包含一个带有 UILabel 的 UITableViewCell,第 2 部分包含一个包含一些 UITextField 的 UITableViewCell。当我从第 1 节(一个 UILabel)中选择一个 UITableViewCell 时,我得到一个对象。有谁知道如何用第 1 节中所选对象的变量填充 UITextFields?
这是一个示例用例:我有一个 UITableView 包含 2 个部分,在第 1 部分中我有学生姓名列表,在第 2 部分中我有一个包含一些 UITextFields 的自定义 UITableViewCell,当我从第 1 部分中选择一个名称时,我想在 UITextFields 中获取该学生的详细信息。
解决方案
您必须使用通知
在 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
}
推荐阅读
- java - 我在 SQLite 中正确使用 Cursor 吗?
- python - 向 Keras 中的嵌入层添加偏差
- javascript - 在 html 的支票簿中添加 if 条件
- python - 将字符串视为列表
- android-studio - 无法解析 Android Studio 上的原始符号错误
- html - PNG图像不显示透明
- rxjs - 保证发射之间的“n”秒,而无需最初等待
- c++ - cmake完成生成文件后如何自动制作
- wordpress - 在 functions.php 中使用 Composer 类
- android - 在编辑文本中按零任意次,但用户应该只能看到 1 位数字,例如计算器