在 Xcode 中保存表视图

发布于 2024-11-04 14:58:22 字数 205 浏览 4 评论 0原文

您好,我需要一些帮助来了解如何保存“用户”对表视图所做的新配置。

我对如何在“AppDelegate.h 和 .m”中完成此操作有点熟悉,但问题是我使用的是基于导航的应用程序,它为您提供了委托和 ViewController。

当用户切换到新视图时,如何对其进行编程以保存新的表视图。有没有一种方法不需要通过 AppDelegate 来完成?

谢谢

Hello I need some help in finding out how to save the new configurations that the "USER" makes to the table view.

I am a little bit familiar on how this is done "in the AppDelegate.h and .m" but the thing is that i am using a Navigation-based app and it gives you the delegate and a ViewController.

How do i program it to save the new table view when the person switches to a new view. Is there a way that one doesnt need to do it through the AppDelegate?

Thank You

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

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

发布评论

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

评论(1

暮色兮凉城 2024-11-11 14:58:22

最好的方法是将 tableView 的数据源保存到文档目录中的 .plist 文件中。然后在 viewWillAppear 方法中,将 tableView 的数据源设置为 .plist 文件(如果可用)。

数据源通常是 NSDictionary/Mutable 或 NSArray/Mutable。它们都有写入文档目录的方法,可以像这样检索:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

将列出的其中之一写入文档目录的方法如下所示:

BOOL saved = [datasource writeToPath:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"] atomically:YES];

if (saved) {
//saved datasource to .plist file.
}
else if (!saved) {
//did not save;
}

要从文档目录加载 plist,首先需要检查看看它是否在那里:

if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"]]) {
         countryMArray = [[NSMutableArray alloc] initWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"]];
 }
 else {
     //it is not there, we need to write it to the documents directory
     [datasource writeToPath:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"] atomically:YES];
 }

祝你好运!

The best way would be by saving the tableView's datasource to a .plist file in the documents directory. Then in your viewWillAppear method, set the tableView's datasource to the .plist file (if it is available).

The datasource is usually an NSDictionary/Mutable or an NSArray/Mutable. They both have the method of writing to the documents directory can be retrieved like so:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

The method of writing one of the listed to the documents directory would be like so:

BOOL saved = [datasource writeToPath:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"] atomically:YES];

if (saved) {
//saved datasource to .plist file.
}
else if (!saved) {
//did not save;
}

To load the plist from the documents directory, you would firstly need to check to see if it is there:

if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"]]) {
         countryMArray = [[NSMutableArray alloc] initWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"]];
 }
 else {
     //it is not there, we need to write it to the documents directory
     [datasource writeToPath:[documentsDirectory stringByAppendingPathComponent:@"savedDatasource.plist"] atomically:YES];
 }

Good luck!

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