Edit:
Ok so basically, I have a UITextView on on view controller, along with a save button. When the save button is tapped, I want the text of the UITextView to be saved to the plist. Then, on a totally separate view controller, I want a UITableView to display the saved files. Hope that makes sense.
Ok,
I hav开发者_运维知识库e been trying this forever and can't get anything to work. My questions are: how do I add a string to a plist programmatically. How do I display that plist in a UITableView?
Thanks,
Tate
this is how you add a NSString to a NSArray which is stored in a plist file.
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *mutableArray = [array mutableCopy];
[mutableArray addObject:@"FooBar"];
[mutableArray writeToFile:path atomically:YES];
[mutableArray release];
But there is no need to save the file to disk every time you add a string. Most likely it is okay to save it to disk when you leave the view and/or when you leave the app.
You know already how to create a NSArray out of a plist file, so you can use this NSArray as datasource for your UITableView, there are dozens of tutorials for this, so I omit this part.
If you use the .plist to get some kind of synchronization between several viewControllers you are doing it wrong. In this case you should ask again.
If you have a pList in you resources you can do it like this:
NSString *file = [[NSBundle mainBundle] pathForResource:@"Workbook1" ofType:@"plist"];
//This will take care of all the data types,
//so in the plist you can have dates, strings, numbers, dictionaries and arrays.
NSArray *array= [NSArray arrayWithContentsOfFile:file];
Once you have your array you can use it as the dataSource for you UITableView
.
To wire to plist you can use the method of most property list objects:
NSString* aString=@"Test";
[aString writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile];
or
NSArray* anArray;
[anArray writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile];
You'll have to load the plist into an array.
Then you'll need to set the data in the cells as a normal table view.
If you want to add strings to the Plist, you'll need to save it to an array and then save the Plist file.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
精彩评论