Dylan
Dylan

Reputation: 1

Saving Table View in Xcode

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

Upvotes: 0

Views: 1089

Answers (1)

max_
max_

Reputation: 24481

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!

Upvotes: 2

Related Questions