2014-01-06 18 views
0

我有一个图片幻灯片应用程序,允许用户评价3张图片。收视率得到存储在一个名为rated这样NSMutableArray为.csv文件格式化NSMutableArray的内容

2014-01-06 07:10:23.040 SlideShowSurvey[50425:70b] (
    1, <-- Rating for Picture 1 
    2, <-- Rating for Picture 2 
    3 <-- Rating for Picture 3 
) 

然后,这是使用下面的代码保存为.csv文件:

-(void)saveRatings 
{ 
    NSString *picRatings = [NSString stringWithFormat:@"%@, \n",self.rated]; 
    // Find documents directory 
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *survey = [docPath stringByAppendingPathComponent:@"pictureRatings.csv"]; 
    // Create new file if none exists 
    if (![[NSFileManager defaultManager] fileExistsAtPath:survey]) 
    { 
     [[NSFileManager defaultManager] createFileAtPath:survey contents:nil attributes:nil]; 
    } 
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:survey]; 
    [fileHandle seekToEndOfFile]; 
    [fileHandle writeData:[picRatings dataUsingEncoding:NSUTF8StringEncoding]]; 
    [fileHandle closeFile]; 
} 

然后我就可以显示该.csv的结果文件添加到UITextView,尽管它显示了数组的结构。例如,多个结果显示为:

(
    1, 
    2, 
    3 
), 
(
    1, 
    2, 
    3 
), 
(
    1, 
    2, 
    3 
) 

有没有一种方法可以格式化数组,使其保存为1,2,3?我能否添加像图片1,图片2,图片3的列标题?例如,我想在UITextView的类似结果显示:

Picture 1, Picture 2, Picture 3 
1,2,3 
1,2,3 
1,2,3 

我试图寻找,但似乎无法找到答案。任何帮助表示赞赏!谢谢。

编辑:我已经解决了这个感谢jrturton的解决方案,使用下面的代码:

 // Set column titles for .csv 
    NSArray *columnTitleArray = @[@"Picture 1", @"Picture 2", @"Picture 3"]; 

    NSString *columnTitle = [columnTitleArray componentsJoinedByString:@","]; 
    NSString *columnTitleToWrite = [columnTitle stringByAppendingString:@"\n"]; 

    // Separate ratings into cells 
    NSString *picRatings = [rated componentsJoinedByString:@","]; 
    NSString *picRatingsToWrite = [picRatings stringByAppendingString:@"\n"]; 

    // Find documents directory 
.. 

然后添加此的方法,以确保列标题是只设置在一个新的文件被创建:

// Create new file if none exists 
    if (![[NSFileManager defaultManager] fileExistsAtPath:survey]) 
    { 
     [[NSFileManager defaultManager] createFileAtPath:survey contents:nil attributes:nil]; 

     // Set title for new file 
     NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:survey]; 
     [fileHandle writeData:[columnTitleToWrite dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
.. 
+0

为什么不使用自定义的UITableCellView来显示 – Retro

+0

我试过这样做,但我只知道如何从'NSMutableArray'本身加载结果,而不是从.csv文件加载结果。我对Objective-C非常陌生。你知道任何代码示例显示如何将.csv加载到'UITableView'中吗? – rosshump

回答

0
NSMutableString *csv = [NSMutableString string]; 

NSString *label = @"\n"; 

[csv appendFormat:@",", label]; 

NSString *picRatings = [rated componentsJoinedByString:csv]; 

我不知道你所期望的appendFormat在上面的代码做,这将可能是养警告,因为label不被使用。在这种情况下,您也不需要可变字符串。

为了得到用逗号分隔的数组的内容,这样做:

NSString *picRatings = [rated componentsJoinedByString:@","]; 

你还需要一个新的行添加到该字符串的结尾,所以你会希望:

NSString *picRatingsToWrite = [picRatings stringByAppendingString:@"\n"]; 

把这个字符串写到你的文件中,你应该没问题。

+0

完美!您的解决方案正是我需要的,谢谢!我想要的最后一件事是有列标题,如图片1,图片2,图片3(如我原来的帖子所示)。你知道我能做到吗?非常感谢你的帮助。 – rosshump

+0

要做到这一点,只要将该字符串写入文件(尾部为\ n),如果这里没有任何内容,或者将其添加到您的第一个评级字符串的开头。 – jrturton

+0

辉煌,我能够在创建文件时设置列标题,我已更新我的帖子以显示此内容。感谢你的帮助! – rosshump

1

这里有一个更简单的方法:

NSMutableString *csv = [NSMutableString string]; 

NSString *label = ...; 

[csv appendFormat:@"%@,\n", label]; 

NSNumber *keyNum = ...; 

[csv appendFormat:@"%d,%d\n", [keyNum intValue], [countNum intValue]]; 

NSString *filename = @"counts.csv"; 

NSError *error; 

[csv writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
+0

请参阅我的更新后的帖子,我使用一些代码尝试解决方案进行了编辑,但我无法使其工作。我不确定你的代码的其他部分是什么,例如'* keyNum'和'countNum'。你可以帮我吗? – rosshump

0

使用这种解析器和获取数据到数组,看看如何利用数据为UITabelViewCellhttps://github.com/davedelong/CHCSVParser

+0

我想过一个CSV解析器,但这个应用程序非常简单,只存储少量的数据在.csv文件中,所以我决定不尝试实现解析器。感谢您的回复。 – rosshump