如何在深入 UITableViewController 结构中恢复用户位置?
我有一个 CoreData 模型,其中包含级别,该级别可以再次包含子级别。
我用列出所有子级别的 UITableViewController
来表示每个级别。当用户点击一行时,新的 UITableViewController
会被推送到 navigationController
上。这里没问题。
我将如何在此表结构中存储用户位置。有这样做的最佳实践吗?如果结构的深度已知,那么我这样做没有问题,但不知何故困惑如何以未定义的深度来处理这个问题。
我应该将用户点击的 NSIndexPath 存储到数组中并将其写入磁盘吗?
I have a CoreData model that contains Levels which can again contain childLevels.
I represent each level with a UITableViewController
listing all childLevels. When a user taps a row a new UITableViewController
is pushed onto the navigationController
. No problem here.
How would I go about storing the user location within this table structure. Is there a best practice for doing this? I have no problem doing this if the depth of the structure was known, but somehow puzzled how to approach this with a undefined depth.
Should I store the NSIndexPath
tapped by the user into an array and write it to disk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
NSIndexPath
作为您的状态并保存/恢复它以实现持久性对我来说很有意义。另外,您的方法(使用存储为属性列表(plist)的 NSArray)应该相当简单。
Using an
NSIndexPath
for your state and saving/restoring it for persistence makes sense to me.Also your approach--to use an NSArray stored as a property list (plist)--should be fairly straightforward.
我没有使用用户点击的 NSIndexPaths,而是使用了底层的 NSManagedObjects,这更安全(以防对象的数量或排序发生变化)并且更快(因为我不需要整个 fetchRequest 和/或视图)。
我对 UINavigationController 进行了子类化并执行了以下操作。
当为某个级别推送新的 TableViewController(存储在
parentLevel
中)时,我将其附加到 UserDefaults 中的一个数组中:当我弹出一个 viewController 时,我只需从该数组中删除最后一个条目:
然后我就可以使用这个数组当应用程序下次启动重建树时初始化 NavigationController 时:
Instead of using the NSIndexPaths tapped by the user I went with the underlying NSManagedObjects which is a lot safer (in case number or sorting of objects change) and faster (because I do not need the whole fetchRequest and or view).
I subclassed the UINavigationController and did the following.
When pushing a new TableViewController for a level (stored in
parentLevel
) I append this to an array in UserDefaults:When I pop a viewController I simply remove the last entry from that array:
I can then use this array when initializing the NavigationController when the app is next started to rebuild the tree:
我正准备开始为我自己的应用程序执行此操作,这是计划。每次 UIViewController 加载新视图时,它都会在 NSUserDefaults 中创建一个值,该值提供有关该新视图的信息(从何处打开它、填充了哪些数据等)。当子视图返回时,视图控制器清除该值。当 UIViewController 进行初始加载时,它会检查默认值以查看之前是否存储了值,如果是,则会采取适当的操作来重新加载该子视图。该过程沿着导航链继续进行。
我这样做而不是 NSIndexPath 的原因是,除了主视图层次结构之外,我还有一堆辅助视图(添加、删除、编辑等)。我不仅需要记录主导航之外存在的这些视图的打开,而且它们还需要保存许多状态详细信息(选定的选项、部分输入的文本等)。
我会回来的如果结果证明这是一个糟糕的计划,请投反对票。
I'm just about ready to start doing this for my own app and here's the plan. Each time a UIViewController loads a new view it will create a value in the NSUserDefaults that gives information on that new view (where it was opened from, what data it's populated with, etc). When the subview returns the view controller clears out this value. When the UIViewController has it's initial load it checks the defaults to see if it previously stored a value, if so, it takes the appropriate action to reload that subview. The process continues down the navigation chain.
The reason I've done this instead of an NSIndexPath is because, in addition to the main view hierarchy, I've got a bunch of auxiliary views (add, remove, edit, etc.). Not only will I need to record the opening of these views that exist outside of the main navigation, but they will have a lot of state details they need to save too (selected options, partially entered text, etc.)
I'll come back here and downvote this if it turns out to be a shit plan.