2013-03-06 3 views
0

Я сделал несколько изменений в plist, таких как вставка, удаление и изменение порядка строк в таблице. Plist переключается на значение по умолчанию в viewDidDisappear.Данные, записанные на Plist, не сохраняются

Я пробовал следующий код.

-(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

Показать код, где вы пытаетесь сохранить plist –

+0

@AnoopVaidya Это последняя строка опубликованного кода. – rmaddy

+0

Какие объекты находятся в массиве? – rmaddy

ответ

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

}

+0

Как устраняется метод 'dataFilePath'? Это все тот же код. – rmaddy

0

Ваш код:

NSArray *revisedArray = [[NSArray alloc]initWithArray:self.categoriesArray]; 
[revisedArray writeToFile:[self dataFilePath] atomically:YES]; 

который правильно !!!

Проблема должна быть от self.categoriesArray, если массив содержит некоторые объекты/пользовательский объект, то вы не можете писать напрямую.

Вам нужно архивировать его NSData

NSData *objData = [NSKeyedArchiver archivedDataWithRootObject:object]; 

Или вы можете ENDCODE его в пользовательский класс.

- (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; 
} 
+0

На самом деле нет необходимости создавать 'revisionArray'. Просто назовите 'writeToFile' на' self.categoriesArray'. – rmaddy

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]; 

пытаются очистить Построить и Перезапустить.

Смежные вопросы