【问题标题】:Saving Table View in Xcode在 Xcode 中保存表格视图
【发布时间】:2011-05-01 23:59:25
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: uitableview xcode4 saving-data


    【解决方案1】:

    最好的方法是将 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];
     }
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多