PDFKIT,Swift如何从uitableview中删除pdf(PDFDocument)

时间:2017-10-02 14:07:45

标签: ios swift uitableview pdfkit nsdocumentdirectory

我有下载PDF文件的代码:

var documents = [PDFDocument]()

  DispatchQueue.global(qos: .default).async(execute: {
            //All stuff here

            print("Download PDF");
            let url=NSURL(string: urlString);
            let urlData=NSData(contentsOf: url! as URL);

            if((urlData) != nil)
            {
                let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

                let fileName = urlString as NSString;

                let filePath="\(documentsPath)/\(fileName.lastPathComponent)";

                let fileExists = FileManager().fileExists(atPath: filePath)

                if(fileExists){

                    // File is already downloaded
                    print("PDF Already Downloaded");
                }
                else{

                    //download
                    DispatchQueue.main.async(execute: { () -> Void in

                        print(filePath)
                        urlData?.write(toFile: filePath, atomically: true);
                        print("PDF Saved");

                        self.refreshData()
                    })
                }
            }
        })

现在我想从表格和文档目录中的uitableview中删除此文件,如何使用索引路径行以及如何查找要删除的文件名

我知道我会删除此处的文件,但我不知道如何在documentDirectory和表中删除PDF

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.delete) {
        // handle delete (by removing the data from your array and updating the tableview)

    }

}

这是我的表格视图单元格

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BookshelfCell

        let document = documents[indexPath.row]
        if let documentAttributes = document.documentAttributes {
            if let title = documentAttributes["Title"] as? String {
                cell.title = title
            }
            if let author = documentAttributes["Author"] as? String {
                cell.author = author
            }

这是我的刷新数据部分

let fileManager = FileManager.default
    let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let contents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
    documents = contents.flatMap { PDFDocument(url: $0) }

1 个答案:

答案 0 :(得分:2)

您需要完成三个步骤才能正确删除文档并更新表格视图:

  1. 使用FileManager' removeItem(at: URL)removeItem(atPath: String)从磁盘中删除文件。 (请注意,这两个方法都抛出,因此您需要使用do-catch块和try,并且只有在方法没有抛出错误时才会继续。)更新:如果您查看documentation for PDFDocument,除了您已经使用的documentAttributes之外,还会发现另一个可选属性documentURL应该可以为您提供删除它所需的内容

  2. documents删除文档(您只需使用现有代码刷新整个数组,但删除单个项目的速度更快)。 documents.remove(at: indexPath.row)

  3. 最后,您需要告诉表视图删除有问题的行(您当然可以重新加载整个表视图,但删除单个单元格更清晰)tableView.deleteRows(at: [indexPath], with .fade)

    < / LI>

    如果您不熟悉do-catch块,请参阅Apple关于Swift的书(请参见下面的链接)中的一些代码:

    do {
        try makeASandwich()
        eatASandwich() // This only gets called if the line above worked
    } catch {
        dealWithTheError() // This only gets called if makeASandwich() throws an error
    }
    

    旁注

    如果你还没有完成,那么Apple对Swift Language提供了很棒的指南,但我建议至少阅读 The Basics 。这将使您对该语言有基本的了解。如果您还是编程新手,我建议您在Swift Playgrounds应用程序中通过iPad上的免费学习Apple代码系列。本系列将指导您完成所有编程基础知识,为您提供搜索Apple提供的文档并找到问题答案的工具。

    我们都从一开始就开始了,在我们行走之前我们都必须爬行才能行走。

相关问题