在Xcode中保存表格视图

| 您好,我需要一些帮助来找出如何将\“ USER \”所做的新配置保存到表视图中。 我对如何“在AppDelegate.h和.m \”中完成此操作有点熟悉,但事实是我使用的是基于导航的应用程序,它为您提供了委托和ViewController。 当人员切换到新视图时,我该如何编程以保存新的表视图。有没有一种方法不需要通过AppDelegate进行? 谢谢     
已邀请:
最好的方法是将tableView \的数据源保存到documents目录中的.plist文件中。然后在viewWillAppear方法中,将tableView \的数据源设置为.plist文件(如果可用)。 数据源通常是NSDictionary / Mutable或NSArray / Mutable。它们都具有写入文档目录的方法,可以这样检索:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
将列出的内容之一写入documents目录的方法如下:
BOOL saved = [datasource writeToPath:[documentsDirectory stringByAppendingPathComponent:@\"savedDatasource.plist\"] atomically:YES];

if (saved) {
//saved datasource to .plist file.
}
else if (!saved) {
//did not save;
}
要从documents目录加载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];
 }
祝好运!     

要回复问题请先登录注册