我对plist做了一些更改,比如在table.plist中插入,删除和重新排序行,在viewDidDisappear上更改为默认值。
我尝试了以下代码。
-(NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataFile = [documentsDirectory stringByAppendingPathComponent:@"ETCategoryList.plist"];
return dataFile;
}
//Code for deleting.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete){
[[self categoriesArray]removeObjectAtIndex:[indexPath row]];
NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
[tableView reloadData];
NSArray *revisedArray = [[NSArray alloc]initWithArray:self.categoriesArray];
[revisedArray writeToFile:[self dataFilePath] atomically:YES];
}
}
答案 0 :(得分:0)
您的代码是:
NSArray *revisedArray = [[NSArray alloc]initWithArray:self.categoriesArray];
[revisedArray writeToFile:[self dataFilePath] atomically:YES];
这是正确的!!!
问题应来自self.categoriesArray
,如果数组包含某些对象/自定义对象,则无法直接编写。
您需要将其存档到NSData
NSData *objData = [NSKeyedArchiver archivedDataWithRootObject:object];
或者您可以在自定义类中对其进行结束编码。
- (void) encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:obj1 forKey:@"key1"];
[encoder encodeFloat:obj2 forKey:@"key2"];
}
- (id)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:coder];
obj1 = [coder decodeObjectForKey:@"key1"];
obj2 = [coder decodeObjectForKey:@"key2"];
return self;
}
答案 1 :(得分:0)
你的代码足够好了。它与我合作愉快。 示例代码:
self.clickMeArray = [[NSMutableArray alloc] init];
for(int i =1; i<10;i++)
{
[self.clickMeArray addObject:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"n%d",i] forKey:[NSString stringWithFormat:@"iN%d",i]]];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataFile = [documentsDirectory stringByAppendingPathComponent:@"MyName.plist"];
[self.clickMeArray writeToFile:dataFile atomically:YES];
尝试清理构建和重新启动。
答案 2 :(得分:-1)
试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *lefttBtn = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain
target:self action:@selector(EditTable:)];
[[self navigationItem] setLeftBarButtonItem:lefttBtn];
[lefttBtn release];
}
- (void) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[table setEditing:NO animated:NO];
[table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[table setEditing:YES animated:YES];
[table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete){
[[self categoriesArray]removeObjectAtIndex:indexPath.row];
[[self categoriesArray] writeToFile:[self dataFilePath] atomically:YES];
[tableView reloadData];
}
}