使用导航栏离开视图时表视图单元格会丢失数据
我有一个包含自定义单元格的表格视图。我的表视图单元格通过插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据对此答案的评论进行编辑以建议更可能的解决方案
假设您将登录视图添加为模式视图控制器,则需要在每次额外登录后刷新表格。在您的 viewWillAppear 方法中重新加载 tableView:
或者,如果您的表需要一段时间才能重新加载,您可以向 viewController 发送通知,以便在关闭之前从登录视图重新加载 tableView。
如果内存紧张,操作系统可以随意卸载当前不可见的视图。一个常见的 iOS 开发错误是假设 viewController 的viewDidLoad
和viewDidUnload
方法中的代码只会在视图首次加载和稍后卸载时调用。检查以确保您的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: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'sviewDidLoad
andviewDidUnload
methods will only be called when the view is first loaded and later unloaded. Check to make sure that yourviewDidLoad
method isn't making any assumptions about the availability of data and that yourviewDidUnload
method isn't accidentally deleting data that the newly re-calledviewDidLoad
will need.Similarly, ensure that your
viewWillAppear
method isn't being thwarted by something in yourviewDidUnload
method.Lastly, and in the same vein, understand that if your view gets dumped by the OS for memory reasons, it will sometimes call yourviewDidUnload
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 itsdidReceiveMemoryWarning
method made it decide to just dump the view immediately and completely, without really allowing it to clean itself up.