2011-05-01 36 views
0

你好,我需要一些幫助來找出如何保存「用戶」對錶格視圖產生的新配置。在Xcode中保存表格視圖

我對「AppDelegate.h和.m」是如何完成這件事有點熟悉,但事實是我正在使用基於導航的應用程序,並且它爲您提供委託和ViewController。

當我切換到新視圖時,如何編程以保存新表格視圖。有沒有一種方法不需要通過AppDelegate來完成?

謝謝

回答

2

,最好的辦法是通過保存的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]; 
} 

祝你好運!

相關問題