2009-12-24 4 views
0

사용자가 숫자 데이터가있는 테이블로 항목을 편집 할 수있게하려면 (사용자가 표 셀을 클릭 한 다음 해당 하위보기에서 번호를 입력하고 메인 테이블 뷰), NSMutabaleArray에 항목을 추가한다고 생각했습니다. 그렇다면 사용자가 응용 프로그램을 종료 할 때 해당 값이 아직 있습니까? 그렇다면 배열을 해제하는 명확한 테이블 메서드가 필요합니까? 감사.NSArray, UITableView

답변

2

앱의 데이터가 자동으로 유지되지 않습니다.

소량의 데이터가있는 경우 배열과 같은 모음을 앱의 Documents 디렉토리에있는 Property List (plist) 파일에 쓸 수 있습니다.

많은 양의 데이터가있는 경우 핵심 데이터를 사용하는 것이 좋습니다.

0

더 빠르고 더러운 방법은 NSArray를 사용하여 NSArray를 파일에 기록하는 것입니다. 그런 다음 응용 프로그램이 시작되면 파일의 데이터를 NSArray로 다시 읽어야합니다.

- (BOOL)loadDataFromDisk 
{ 
    NSString* path = [self pathForDataFile]; 
    NSFileManager* manager = [NSFileManager defaultManager]; 

    if ([manager fileExistsAtPath: path]) 
    { 
     NSLog(@"Saved data found, loading data from %@", path); 

     NSMutableDictionary* rootObject = nil; 
     rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile: path]; 

     NSArray* self.yourArray = [rootObject objectForKey: kYourKey]; 

    } 
    else 
    { 
     NSLog(@"No saved data, initializing objects with default values."); 
    } 

    return YES; 
} 

참고 '-

- (BOOL)saveDataToDisk 
{ 
    NSString* path = [self pathForDataFile]; 

    NSMutableDictionary* rootObject = [NSMutableDictionary dictionary]; 

    if (self.yourArray) 
     [rootObject setValue: self.powerListCollection.items forKey: kYourKey]; 

    [NSKeyedArchiver archiveRootObject: rootObject toFile: path]; 

    return YES; 
} 

:

- (NSString*)pathForDataFile 
{ 
    //Get the path of the Documents directory 
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex: 0]; //Path to the documents directory 

    //Get the path to my file in /Documents 
    NSString* documentsPath = [documentsDirectory stringByAppendingPathComponent: 
           [NSString stringWithFormat: @"%@.plist", @"FileName"]]; 

    return documentsPath; 
} 

이 저장하고로드 :

이 문서 경로를 가져옵니다 여기

은 조각이다 w를 유지해야합니다. objectForKey에서 가져온 것 :