简体   繁体   English

子控制器在父表视图中更新单元格的标签

[英]Child controller updating a label for a cell in parent table view

I have a navigation view with a cell that has something as simple as a Date of Birth. 我有一个导航视图,其中包含一个像出生日期一样简单的单元格。 When that particular row is clicked, a new controller get pushed into view with a UIDatePicker. 单击该特定行时,新控制器将使用UIDatePicker进入视图。 Once a new date is selected, I'd imagine I need to do something in viewWillDisappear: for that particular cell's UITextField value. 一旦选择了新日期,我想我需要在viewWillDisappear中执行一些操作:对于该特定单元格的UITextField值。 What's the best way for me to do that? 对我来说最好的方法是什么? Do I need to pass in a reference to the cell's indexPath in the childController, set it there, and reload the tableView's data? 我是否需要在childController中传入对单元格的indexPath的引用,将其设置在那里,然后重新加载tableView的数据?

Another tricky tidbit is let's say that my data source for the tableView is a dictionary with key/value pairs. 另一个棘手的问题是让我说tableView的数据源是一个带键/值对的字典。 When I call reloadData on the tableView, it'll re-read that dictionary without taking into note the date change my child controller has made. 当我在tableView上调用reloadData时,它会重新读取该字典,而不会记录我的子控制器所做的日期更改。

First, the best place to reload data is in that particular UIViewController's viewWillAppear: method. 首先,重新加载数据的最佳位置是特定的UIViewController的viewWillAppear:方法。 That way you don't need to worry about references to the table in other controllers. 这样您就不必担心在其他控制器中对表的引用。

Second, if I'm not misunderstanding, I think you need to brush up on objects. 其次,如果我没有误会,我认为你需要了解对象。 As long as what you change in the editing (child) controller is a reference to the same object that the table (parent) controller uses, the data will be synced. 只要您在编辑(子)控制器中更改的内容是对表(父)控制器使用的同一对象的引用,就会同步数据。 If, on the other hand, you're reloading data (say, from a file) for each controller, I'd take a look at how your app architecture works. 另一方面,如果您正在为每个控制器重新加载数据(例如,从文件中),我将了解您的应用程序架构的工作原理。 Objects are your friend. 物体是你的朋友。 Take advantage of that. 利用这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用表格视图单元格中的文本并将其显示为父视图控制器中的标签? - How can I use text from a table view cell and display it as a label in a parent view controller? 在容器视图中选择表格单元格时与父视图控制器进行交互 - Interact with Parent view controller when selecting a table cell in a container view 将所有子值检索到表视图单元格中的标签 - Retrieve all child value to a label in table view cell NSDictionary更新视图控制器的标签 - Updating Label of a View Controller by a NSDictionary 拆分视图控制器折叠/展开时更新表单元格附件 - Updating Table Cell Accessory when Split View Controller Collapses/Expands iOS:如何将表视图单元格的第一个视图控制器标签数据传递给第二个视图控制器的textView? - iOS: How to pass first view controller label data of table view cell to textView of second view controller? 将子控制器添加到表视图单元时,不会调用viewWillAppear - viewWillAppear is not called when add child controller to table view cell 父控制器阻止子表视图UITableViewRowAction swift - Parent Controller blocking child table view UITableViewRowAction swift 表单元进入视图控制器 - Table cell into view controller 父视图控制器中的子视图控制器 - Child View Controller within Parent View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM