返回时重新加载 UITableView?
我有一个包含 UITableView
的顶级 UIViewController
。顶层 UIViewController
实例化一个 NavigationController
,并将另一个 UIViewController
推送到 NavigationController
上。 IE 我们进入了一个新的第二视图。第二个视图的左上角有通常的“后退”按钮,可让您导航回顶层视图。
当从第二个视图导航回顶级视图时,是否可以通过调用 cellForRowAtIndexPath
使用第二个视图中生成的数据在顶级视图中重绘 UITableView
code> 在顶层,如果是这样,如何做到这一点?
I have a top level UIViewController
that contains a UITableView
. The top level UIViewController
instantiates a NavigationController
, and pushes another UIViewController
onto the NavigationController
. I.E. we push into a new, second view. This second view has the usual "Back" button in the upper left hand corner to allow you to navigate back to the top level view.
Is it possible, when navigating back to the top level view from the second view, to redraw the UITableView
in the top level view, using data generated in the second view, by calling cellForRowAtIndexPath
in the top level and if so, how does one do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要做的就是这样(在具有 UITableView 的 UIViewController 中)。此时您不必担心细胞水平上会发生什么。
只需将上面的代码添加到您的控制器中,当您从第二个视图返回时,您就会看到正确的事情发生了。对
reloadData
的调用告诉表视图它需要刷新模型中的单元格,其他一切都会顺利进行。All you need to do is this (in your UIViewController that has a UITableView). You don't have to worry about what happens at cell-level at this point.
Just add the code above to your controller, and you'll see that the right thing happens when you come back from your second view. The call to
reloadData
tells the table view that it needs to refresh its cells from your model, and everything else follows nicely.如果您只想重新加载选定的单元格,请在
UITableViewController
的自定义子类中重写viewWillAppear:
,如下所示:注意:假设您已离开
clearsSelectionOnViewWillAppear< /code> 设置为
YES
(默认),您必须在调用super
之前获取所选行的索引路径,这会清除选择。此外,@ZoranSimic 的解决方案仅调用
[self.tablView reloadData]
是可以接受的,因为它的代码更少,但仍然高效。最后,也许保持表格视图的单元格与它们所代表的模型对象同步的最佳方法是像 NSFetchedResultsController 那样进行操作并使用键值观察(KVO)和/或 NSNotification code> 当模型对象发生更改时通知您的表视图控制器,以便它可以重新加载相应的单元格。表视图控制器可以在
viewDidLoad
中开始观察其模型对象的变化,并在dealloc
中结束观察(以及任何手动卸载self.view< /代码>
)。
If you only want to reload the cell that was selected, override
viewWillAppear:
in your custom subclass ofUITableViewController
like so:NOTE: Assuming you've left
clearsSelectionOnViewWillAppear
set toYES
(the default), you must get the index path of the selected row before callingsuper
, which clears the selection.Also, the solution of @ZoranSimic to just call
[self.tablView reloadData]
is acceptable as it's less code and still efficient.Finally, perhaps the best way to keep your table view's cells in sync with the model objects they represent is to do like
NSFetchedResultsController
and use key-value observing (KVO) and/orNSNotification
to inform your table view controller when model objects have changed so that it can reload the corresponding cells. The table view controller could begin observing changes to its model objects inviewDidLoad
and end observing indealloc
(and anywhere you manually unloadself.view
).除了 Zoran Simic 的回答之外,还有 Swift 代码:
In addition to the answer of Zoran Simic here the Swift code:
您可以实现 UINavigationControllerDelegate 的方法并创建弹出视图控制器时刷新的逻辑。
另一种方法是在表视图控制器上实现 viewWillAppear 上的一些逻辑,以根据弹出的视图控制器的数据进行刷新。
如果您需要将数据从第二个视图控制器发送到第一个视图控制器,请记住您的第二个视图控制器“不”知道前一个视图控制器的存在。它应该是透明的。您应该实现一些协议以使第二个视图控制器将数据发送到第一个视图控制器。这将是解决此问题的第三个选项,因为您的第一个视图控制器将是委托,第二个视图控制器将向委托发送信息,您可以根据以下内容准备要重新加载的表视图(第一个视图控制器)收到数据。
You could implement the methods of
UINavigationControllerDelegate
and create the logic for the refreshing when popping view controllers.Another way to do this would be implementing on your table view controller some logic on
viewWillAppear
, to refresh based on the data of the popped view controller.If you need to send data from the second view controller to the first view controller, remember that your second view controller "doesn't" know the existence of the previous view controller. It should be transparent for it. You should implement some protocol to make the second view controller send data to the first view controller. It would be a third option to this problem, since your first view controller would be the delegate and the second view controller would be sending info to the delegate, you could be preparing your table view (first view controller) to reload, based on the received data.
每次视图出现时(包括第一次)都会调用
viewWillAppear
。这是我的解决方案,仅当您导航回来时才重新加载视图
我在
ViewController
的.h 文件
中创建一个变量,以检查这是我第一次进入此页面ViewController
或当我导航回来之后在
viewWillAppear
希望这有帮助
viewWillAppear
gets called every time the view appears include the first time.Here is my solution for reload the view only when you navigate back
I create one variable in
.h file
of myViewController
for checking that it is the first time I go to thisViewController
or when I navigate backAfter that in
viewWillAppear
Hope this help
也许现在可以提供帮助。
对于 Swift 2+
在 viewDidAppear 方法中:
如果您想重新加载所有表格:
如果您只想重新加载选定的单元格。
Maybe can help now.
For Swift 2+
In your viewDidAppear method:
If you want to reload all table:
If you want to reload only the cell selected.
更新为 Swift 3
Updated for Swift 3