我有一個使用NSMutableArray填充的UITableView。我正在使用xcode 4.2保存NSMutable陣列數據
NSMutable陣列中的數據保持在我切換應用程序的情況下,但在以下兩種情況下它被擦除: 1-用戶切換視圖並返回。
2的應用是completley關閉(即在主按鈕,用戶雙擊並運行應用程序的列表中刪除)
這裏要說的是,我使用
-(NSString *)dataFilePath{
NSString *dataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
dataFilePath = [documentDirectory stringByAppendingPathComponent:@"Test App-Info.plist"];
return dataFilePath;
}
-(void)saveData{
[NSKeyedArchiver archiveRootObject:[data copy] toFile:[self dataFilePath]];
}
- (void)loadData
{
data = [NSKeyedUnarchiver unarchiveObjectWithFile:self.dataFilePath];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
...
//saving the history
NSArray *archivedArray =[NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
if (archivedArray == nil) {
data = [[NSMutableArray alloc] init];
}
else {
[self loadData];
[mainTableView reloadData];
}
}
代碼請讓我知道如果我失去了一些東西
感謝
編輯:
個保存數據的功能加載在兩個位置: 1的應用程序,我發展掃描QR碼,所以保存的數據是所謂的如下功能:
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
....
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results){
// EXAMPLE: just grab the first barcode
break;
}
if(!symbol)
return;
// EXAMPLE: do something useful with the barcode data
theBarcodeString = symbol.data;
//adding the string to the list
[data addObject:theBarcodeString];
[self saveData];
[mainTableView reloadData];
[self endText];
stringLabel.text=theBarcodeString;
...
}
它也被稱爲在當數據編輯:
-(IBAction)editTable{
UIBarButtonItem *leftItem;
[mainTableView setEditing:!mainTableView.editing animated:YES];
if (mainTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)];
}
else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)];
}
[self saveData];
[mainTableView reloadData];
}
因此,您從哪裏保存數據? – Denis
我可能是錯的(我是App開發的新手)但根據代碼,數據保存在Test App-Info.plist(項目plist)的dataFilePath中,是否應該將其更改爲其他內容? – user1051935
我的意思是你用什麼方法調用saveData方法? – Denis