import UIKit import PDFKit import MobileCoreServices import UniformTypeIdentifiers
class ViewController: UIViewController, PDFViewDelegate, UIDocumentPickerDelegate, UITableViewDataSource, UITableViewDelegate { let pdfView = PDFView() var PDFFiles: [URL] = [] var tableView: UITableView! var tableDataSource: MyTableDataSource!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "test"
self.navigationController?.setNavigationBarHidden(false, animated: false)
view.backgroundColor = UIColor.white
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Import", image: UIImage(systemName: "square.and.arrow.down"), target: self, action: #selector(importButton))
// TableView Setup
tableView = UITableView(frame: view.bounds)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableDataSource = MyTableDataSource()
tableView.dataSource = tableDataSource
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "PDFCell")
tableView.delegate = self
view.addSubview(tableView)
}
@objc private func importButton() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.data], asCopy: true)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = true
present(documentPicker, animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
for url in urls {
print("Imported file URL: \(url)")
PDFFiles.append(url)
}
print("Numbers of files after import: \(PDFFiles.count)")
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PDFFiles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PDFCell", for: indexPath)
let fileName = PDFFiles[indexPath.row].lastPathComponent
cell.textLabel?.text = fileName
return cell
}
}
This is my code and I am getting this error. The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method} can I get some help?