0

在我的应用程序中,我选择文件UIDocumentPicker并将文件名放在 tableView. 单击 acell时,我希望应用程序打开文件。我不知道如何打开之前挑选的文件。请帮忙。

import UIKit

extension ViewController: UIDocumentMenuDelegate {
    func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self

        self.presentViewController(documentPicker, animated: true, completion: nil)
    }
}

extension ViewController: UIDocumentPickerDelegate {
    func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
        if controller.documentPickerMode == UIDocumentPickerMode.Import {
          dispatch_async(dispatch_get_main_queue()) {

              if let fileName = url.lastPathComponent {

                self.files.append(fileName)

            }

          }            
        }      
    }        
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var files = [AnyObject]()

    @IBOutlet weak var fileTableView: UITableView!
    @IBAction func addDocuments(sender: AnyObject) {
        let importMenu = UIDocumentMenuViewController(documentTypes: ["public.data", "public.text"], inMode: .Import)

        importMenu.delegate = self

        self.presentViewController(importMenu, animated: true, completion: nil)

        let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.data", "public.text"], inMode: .Import)
        documentPicker.delegate = self
        documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen

        self.presentViewController(documentPicker, animated: true, completion: nil)
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    }
4

1 回答 1

2

您需要保留对整个 URL 的引用,而不仅仅是文件名。

将完整添加urlself.files. 然后更新您cellForRowAtIndexPath以仅显示lastPathComponent该 URL 的。

然后,didSelectRowAtIndexPath您可以访问文件的完整 URL。

于 2016-05-15T14:40:59.290 回答