0

我对 plist 进行了一些更改,例如在 table.plist 中插入、删除和重新排序行。plist 在 vi​​ewDidDisappear 上更改为默认值。

我尝试了以下代码。

-(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];
}
}
4

3 回答 3

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;
}
于 2013-03-06T06:58:08.607 回答
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];

尝试清理构建和重新启动。

于 2013-03-06T08:07:38.553 回答
-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];
}

}

于 2013-03-06T06:53:10.790 回答