0
我對這個問題有點搜索,但似乎找不到解決方案,所以希望你能幫助。顯示上次提交的.csv文件的數據?
基本上我已經構建了一個應用程序,可以將客戶輸入數據保存到.csv文件。現在我希望在應用程序中顯示這些信息,但只顯示最後輸入的用戶數據(用戶的其他數據需要保存到.csv中,但不需要在數據庫稍後才能查看)我可以加載它所有的數據,但我只想要最後提交的數據顯示在應用程序中...這是我的代碼到目前爲止;
-(IBAction)saveinfo:(id)sender{
NSString *resultLine=[NSString stringWithFormat:@"%@ , %@ , %@ , %@ , %@ , %@, %@\n\n\n\n\n\n\n",
self.customername.text,
self.houseno.text,
self.customerpostcode.text,
self.catno.text,
_ordervalresult.text,
_reordervalresult.text,
self.date.text
];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//resultview.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"customerdata.csv"];
if (![[NSFileManager defaultManager] fileExistsAtPath:surveys]) {
[[NSFileManager defaultManager]
createFileAtPath:surveys contents:nil attributes:nil];
}
NSFileHandle *filehandle = [NSFileHandle fileHandleForUpdatingAtPath:surveys];
[filehandle seekToEndOfFile];
[filehandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
[filehandle closeFile];
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
[email protected]"";
_orderval.value=0;
_reorderval.value=0;
_ordervalresult.tex[email protected]"£0.00";
[email protected]"£0.00";
NSLog(@"surveys path: %@", surveys);
}
-(IBAction)retrieveinfo:(id)sender {
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//resultView.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"customerdata.csv"];
if([[NSFileManager defaultManager] fileExistsAtPath: surveys])
{
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveys];
NSString *surveyResults =[[NSString alloc]initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
[fileHandle closeFile];
self.resultview.text=surveyResults;
}
}
任何人都知道如何解決這個問題?我已經嘗試了一些想法,但可以只加載所有的信息,或者根本就沒有...
謝謝您的時間
編輯:更新的代碼
-(IBAction)retrieveinfo:(id)sender {
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
_resultview.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"customerdata.csv"];
if([[NSFileManager defaultManager] fileExistsAtPath: surveys])
self.resultview.text=surveys;
//This is a part of your code from retrieveinfo:sender:
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveys];
NSString *surveyResults =[[NSString alloc]initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
[fileHandle closeFile];
//All lines of you CSV file
NSArray *allLines = [surveyResults componentsSeparatedByString:@"\n"];
//To get Last Line
NSString *lastEntry = [allLines lastObject]; //or [allLines firstObject]; depending on the order your build it, but if I understood correctly it should be lastObject
NSArray *lastData = [lastEntry componentsSeparatedByString:@","];
NSString *lastCustomerName = [lastData objectAtIndex:0];
NSString *lastHouseNo = [lastData objectAtIndex:1];
NSString *lastCustomerPostcode = [lastData objectAtIndex:2];
嗨那太好了,我可以看到你是如何試圖提取的信息,但我已編輯的代碼,但現在越來越未使用的變量和數據不被重新載入?對不起,要成爲一個痛苦,但你可以看看,看看我哪裏出錯了? (我編輯了原帖) – user3788098 2014-09-10 14:00:38
@ user3788098:我編輯了我的答案。 – Larme 2014-09-10 14:10:34
輝煌的它現在的作品! Theres只有一個問題,當[allLines firstObject];它工作並加載數據庫中的第一個。你是正確的,但我希望在數據庫中有最後一個,但是當它的[allLines lastObject];我收到錯誤消息@autoreleasepool { return UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); } – user3788098 2014-09-10 15:33:33