2013-03-06 4 views
0

table.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

사실,'revisedArray'를 만들 필요가 없습니다. 'self.categoriesArray'에서'writeToFile'을 호출하기 만하면됩니다. – 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]; 

빌드 및 재시동을 시도하십시오.

관련 문제