使用导航栏离开视图时表视图单元格会丢失数据

发布于 2024-11-07 20:34:07 字数 131 浏览 1 评论 0原文

我有一个包含自定义单元格的表格视图。我的表视图单元格通过插入 SQLite 数据库中的值进行填充。

数据库值显示在我的表视图单元格中,但问题是,当我从表视图导航到上一个视图,然后通过导航栏返回表视图时,表视图变为空。可能是什么问题?

I have a table view which has custom cells. My table view cell get populated through the values inserted in an SQLite database.

The database values are shown in my table view cells, but the problem is, when I navigate from my table view to the previous view, and back to the tableview through the navigation bar, the table view becomes empty. What might be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-11-14 20:34:07

根据对此答案的评论进行编辑以建议更可能的解决方案

假设您将登录视图添加为模式视图控制器,则需要在每次额外登录后刷新表格。在您的 viewWillAppear 方法中重新加载 tableView:

[tableView reloadData];

或者,如果您的表需要一段时间才能重新加载,您可以向 viewController 发送通知,以便在关闭之前从登录视图重新加载 tableView。


如果内存紧张,操作系统可以随意卸载当前不可见的视图。一个常见的 iOS 开发错误是假设 viewController 的 viewDidLoadviewDidUnload 方法中的代码只会在视图首次加载和稍后卸载时调用。检查以确保您的 viewDidLoad 方法不会对数据的可用性做出任何假设,并且您的 viewDidUnload 方法不会意外删除新重新调用的数据viewDidLoad 需要。

同样,请确保您的 viewWillAppear 方法不会受到 viewDidUnload 方法中的某些内容的阻碍。

最后,同样,请理解,如果您的视图因内存原因被操作系统转储,它有时会在清除视图时调用您的 viewDidUnload 方法,但有时似乎不会调用它,也许是因为视图使用的内存太大,以至于未能清除其 didReceiveMemoryWarning 方法中的内容,导致它决定立即完全转储视图,而不真正允许它清理自己起来。

Edited to suggest a more likely solution based on comments to this answer

Assuming you're adding the login view as a modal viewController, you need to refresh the table after each additional login. In your viewWillAppear method reload your tableView:

[tableView reloadData];

Or, if your table takes a while to reload, you can send a notification to the viewController to reload the tableView from the login view before you dismiss it.


Currently-not-visible views can be unloaded at-will by the OS if memory gets tight. A common iOS development mistake is to assume that the code in a viewController's viewDidLoad and viewDidUnload methods will only be called when the view is first loaded and later unloaded. Check to make sure that your viewDidLoad method isn't making any assumptions about the availability of data and that your viewDidUnload method isn't accidentally deleting data that the newly re-called viewDidLoad will need.

Similarly, ensure that your viewWillAppear method isn't being thwarted by something in your viewDidUnload method.

Lastly, and in the same vein, understand that if your view gets dumped by the OS for memory reasons, it will sometimes call your viewDidUnload method as it clears it out, but sometimes seems to not call it, perhaps because the memory used by the view was so great that a failure to clear things out in its didReceiveMemoryWarning method made it decide to just dump the view immediately and completely, without really allowing it to clean itself up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文