PDF视图不显示

问题描述 投票:0回答:1

我正在从网址下载PDF,然后在Swift5中使用PDFKit显示它。但是,当我点击viewCell时,它不会显示PDF,也可能不会下载PDF。如何验证?

我有两个文件,PlanogramViewController和ShowPDFViewController。

var pdfURL: URL!

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let plano = self.data[indexPath.row] as? Planogram {
            guard let url = URL(string: Plano.pdfUrl) else { return }

            let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())

            let downloadTask = urlSession.downloadTask(with: url)
            downloadTask.resume()

            let pdfViewController = ShowPDFViewController()
            pdfViewController.pdfURL = self.pdfURL
            present(pdfViewController, animated: false, completion: nil)
            tableView.deselectRow(at: indexPath, animated: true)
        }

    }

extension PlanogramViewController:  URLSessionDownloadDelegate {
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("downloadLocation:", location)
        // create destination URL with the original pdf name
        guard let url = downloadTask.originalRequest?.url else { return }
        let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
        let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
        // delete original copy
        try? FileManager.default.removeItem(at: destinationURL)
        // copy from temp to Document
        do {
            try FileManager.default.copyItem(at: location, to: destinationURL)
            self.pdfURL = destinationURL
        } catch let error {
            print("Copy Error: \(error.localizedDescription)")
        }
    }
}

因此,我的扩展程序没有任何下载验证。它应该打印我的初始下载位置,然后将其移到缓存中。如果遇到错误,它也应该打印错误,但是控制台中什么也没有。好像什么都没有下载。

然后是下载了PDF的pdfViewController:

import UIKit
import PDFKit


class ShowPDFViewController: UIViewController {

    var pdfView = PDFView()
    var pdfURL: URL!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(pdfView)

        if let document = PDFDocument(url: pdfURL) {
            pdfView.document = document
        }

        DispatchQueue.main.asyncAfter(deadline: .now()+3) {
            self.dismiss(animated: true, completion: nil)
        }
    }

    override func viewDidLayoutSubviews() {
        pdfView.frame = view.frame
    }
}

当前的行为是仅突出显示我点击的单元格。控制台中没有任何内容,显示屏上也没有任何内容。

swift xcode pdf pdfkit swift5
1个回答
0
投票

您需要在具有tableView的视图控制器中遵守UITableViewDelegateUITableViewDataSourceDelegate协议。

并添加

tableView.delegate = self
tableView.dataSource = self

在viewDidLoad()中>

然后,实现必需的方法,并添加可选方法以进一步自定义tableView。

© www.soinside.com 2019 - 2024. All rights reserved.