Search code examples
iosswiftuitableviewstaticcell

Xcode/Swift 4 Static table cells that open pdf files


I understand the concept of passing data between VCs. I would like to know how to ensure a specific row of section should be referenced to open a particular file and if my use of PDFKit is correct.

I am having a hard time figuring out how to manage table cell selections to open pdf files in a new view controller without having hundreds of segues in my storyboard. This is a pic of my storyboard: (https://i.ibb.co/jL7nvhv/Screen-Shot-2019-05-07-at-2-41-42-PM.png)

I have previously used drag and drop segues from cells to view controllers but that is time-consuming and seems to slow down the app. In the example I have provided, I wish to have the selected cells open a pdf file describing that cell in more detail.

There is a segue between the view controllers called "detailSegue". The 'Transport Detail VC' has a UIView on top of it with a 'PDFView class'. There is no section or row data in the 'Transport Table VC' as I constructed the table from the storyboard.

//Transport VC (where i want PDF's to display):
import UIKit
import PDFKit

class TransportViewController: UIViewController {

    @IBOutlet weak var pdfView: PDFView!
    override func viewDidLoad() {
        super.viewDidLoad()

        if let path = Bundle.main.path(forResource: "", ofType: "pdf"){
            if let pdfDocument = PDFDocument(url:        URL(fileURLWithPath:path)) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical
                pdfView.document = pdfDocument
            }
        } 
    }
}
//Transport Table VC

import UIKit

class TableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

I have no idea where to start. If I dont figure out an alternative I will go back to the 100 segue method.

Ideally I would like to have each cell open up their own pdf file(stored locally and named accordingly i.e ferari.pdf etc...) and have it displayed on the Transport detail VC.


Solution

  • In the below sample I got name of the file in table did select method and pass it to TransportViewController via prepare for segue method

    Please find the dropbox link for code

    https://www.dropbox.com/s/2697rzajiu2vmm0/PDFSample.zip?dl=0

    Note : Only for Toyota I added the PDF file in project