1

我有一个从 S3 帐户下载的 CSV 文件,我想使用 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];
}
4

1 回答 1

3

您不会将预览控制器的视图添加到您的视图中 - 您呈现预览控制器。您应该设置数据源后执行此操作!所以,按照这个顺序:

QLPreviewController* preview = [QLPreviewController new];
preview.dataSource = self;
[self presentViewController:preview animated:YES completion:nil];
于 2013-05-21T01:29:04.057 回答