Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios PDFKIT,Swift如何从uitableview中删除pdf(PDFDocument)_Ios_Swift_Uitableview_Pdfkit_Nsdocumentdirectory - Fatal编程技术网

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

Ios PDFKIT,Swift如何从uitableview中删除pdf(PDFDocument),ios,swift,uitableview,pdfkit,nsdocumentdirectory,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(cont

我有以下下载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和Table中的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) }

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

  • 使用
    FileManager
    removietem(at:URL)
    removietem(atPath:String)
    从磁盘删除文件。(请注意,这两种方法都会引发错误,因此您需要将do catch块与
    try
    一起使用,并且只有在该方法未引发错误的情况下才能继续。)更新:如果查看,您会发现除了您已经在使用的
    文档属性之外,还有另一个可选属性,
    documentURL
    ,它应该为您提供删除它所需的确切信息

  • 文档
    中删除文档(您可以使用现有代码刷新整个数组,但删除单个项目会更快)<代码>文档.删除(位于:indexPath.row)

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

  • 如果您不熟悉do catch块,这里有一段苹果关于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
    }
    
    旁注 苹果公司有一本很棒的指南,如果你还没有这样做的话,我建议你至少读一下基础知识。这将使你对语言有一个基本的了解。如果你对编程也不熟悉,我建议你在Swift Playgrounds应用程序中浏览苹果公司在iPad上免费提供的Learn to Code系列。本系列将指导您了解编程的所有基础知识,为您提供工具来搜索Apple提供的文档并找到问题的答案


    我们都是从一开始的某个时候开始的,我们都必须在走路之前爬,在跑步之前爬得很好。

    如果你能更好地解释一下你的表视图设置,人们将能够更好地帮助你。你的建议是什么?我很好地更新了代码@Matthew Seaman?我看到你添加了一些东西。应该更有帮助。你能找到一个从NSDirectory@matthewseman中完全删除的解决方案吗?请看@theMikeSwan的回答谢谢@theMikeSwan你能写一个完全正确的代码吗?因为我是瑞士人,我需要在这部分删除项中找到从表视图中删除的url(网址:url)你能帮助我吗?我很好添加了这部分,documents.remove(at:indexPath.row),refreshData()但对于removietem(at:URL),我如何调用FileManager,如何为加载项找到fid路径名或URL地址如果可以为此编写代码,那么对我来说@Themikeswan是如此复杂,以至于可以找到路径或URL(FileManager)使用索引路径时,您必须以与获取
    文档属性相同的方式获取URL。如果let documentAttribute=document.DocumentAttribute,则使用
    ,而不是像对属性所做的那样使用
    如果let documentURL=document.documentURL