在本地保存后,如何在IOS中加载CSV

时间:2013-05-21 01:22:08

标签: ios objective-c quicklook

我有一个CSV文件,我正在从S3帐户下载,我想通过使用Quicklook框架在我的ios应用程序中显示它。

我得到的错误是在我的控制台中。它说

QLPreviewController's datasource shouldn't be nil at this point.

此代码行运行// Set data source [previewer setDataSource:self];

后出现

这里是下载文件的所有代码,保存它然后加载quicklook

-(void)showDocument
{
    NSString *stringURL = @"http://jornada.s3.amazonaws.com/Dust.csv";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];
    if ( urlData )
    {
        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"tempfile.csv"];

        //[urlData writeToFile:filePath atomically:YES];

        BOOL newFile = [[NSFileManager defaultManager] createFileAtPath:filePath contents:urlData attributes:nil];

        arrayOfDocuments = [[NSArray alloc] initWithObjects:
                            filePath, nil];


        QLPreviewController *previewer = [[QLPreviewController alloc] init];

        [self addSubview:previewer.view];

        // Set data source
        [previewer setDataSource:self];

        // Which item to preview
        [previewer setCurrentPreviewItemIndex:0];
    }

}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return [arrayOfDocuments count];
}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{


    // Break the path into it's components (filename and extension)
    NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

    NSArray *filePaths = [[fileComponents objectAtIndex:0] componentsSeparatedByString:@"/"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[((NSString *)[filePaths objectAtIndex:[filePaths count]-1]) stringByAppendingString:(NSString*)[fileComponents objectAtIndex:1]]];

    // Use the filename (index 0) and the extension (index 1) to get path

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];


    if (fileExists) {
        //
    }
    return [NSURL fileURLWithPath:filePath isDirectory:NO];
}

1 个答案:

答案 0 :(得分:3)

您不会在视图中添加预览控制器的视图 - 您将呈现预览控制器。你应该在设置数据源之后做到这一点!所以,按此顺序:

QLPreviewController* preview = [QLPreviewController new];
preview.dataSource = self;
[self presentViewController:preview animated:YES completion:nil];