Search code examples
iosiphoneswiftquicklook

QuickLook class not working


I have this class which works fine

import UIKit import QuickLook

class ViewController: UITableViewController, QLPreviewControllerDataSource {

    var urlList : [NSURL]? = {
        if let fileURL1 = NSBundle.mainBundle().URLForResource("Essay", withExtension:"txt"),
            let fileURL2 = NSBundle.mainBundle().URLForResource("Image", withExtension:"jpg"),
            let fileURL3 = NSBundle.mainBundle().URLForResource("Letter", withExtension:"docx"),
            let fileURL4 = NSBundle.mainBundle().URLForResource("Newsletter", withExtension:"pages"),
            let fileURL5 = NSBundle.mainBundle().URLForResource("Presentation", withExtension:"key"),
            let fileURL6 = NSBundle.mainBundle().URLForResource("VisualReport", withExtension:"pdf"),
            let fileURL7 = NSBundle.mainBundle().URLForResource("aaa", withExtension:"x")
        {
                return [ fileURL1 , fileURL2, fileURL3, fileURL4, fileURL5, fileURL6, fileURL7 ]
        }
        return nil
    }()

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


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let list = urlList {
            return list.count 
        }
        return 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("default", forIndexPath: indexPath) 
        if let list = urlList, let fileName = list[indexPath.row].lastPathComponent {
            cell.textLabel?.text = fileName
        }
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let previewQL = QLPreviewController() 
        previewQL.dataSource = self as QLPreviewControllerDataSource 
        previewQL.currentPreviewItemIndex = indexPath.row 
        showViewController(previewQL, sender: nil) 
    }

    func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
        if let list = urlList {
            return list.count
        }
        return 0
    }

    func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {
        var fileURL : NSURL?
        if let list = urlList, let filePath = list[index].lastPathComponent {
            fileURL = NSBundle.mainBundle().URLForResource(filePath, withExtension:nil)
        }

        print(fileURL!)
        return fileURL!
    }
}

but when I change the Quick look to be another class as follows it stops working?

import UIKit
import QuickLook

class ViewController: UITableViewController {

    var urlList : [NSURL]? = {
        if let fileURL1 = NSBundle.mainBundle().URLForResource("Essay", withExtension:"txt"),
            let fileURL2 = NSBundle.mainBundle().URLForResource("Image", withExtension:"jpg"),
            let fileURL3 = NSBundle.mainBundle().URLForResource("Letter", withExtension:"docx"),
            let fileURL4 = NSBundle.mainBundle().URLForResource("Newsletter", withExtension:"pages"),
            let fileURL5 = NSBundle.mainBundle().URLForResource("Presentation", withExtension:"key"),
            let fileURL6 = NSBundle.mainBundle().URLForResource("VisualReport", withExtension:"pdf"),
            let fileURL7 = NSBundle.mainBundle().URLForResource("aaa", withExtension:"x")
        {
                return [ fileURL1 , fileURL2, fileURL3, fileURL4, fileURL5, fileURL6, fileURL7 ]
        }
        return nil
    }()

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


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let list = urlList {
            return list.count // 2
        }
        return 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("default", forIndexPath: indexPath) 
        if let list = urlList, let fileName = list[indexPath.row].lastPathComponent {
            cell.textLabel?.text = fileName // 3
        }
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        _  = PreviewController(controller: self, documentURL: urlList![indexPath.row])
    }
}

and

import UIKit
import QuickLook

class PreviewController: NSObject, QLPreviewControllerDataSource {

    var documentURL: NSURL

    init(controller: ViewController, documentURL: NSURL) {
        self.documentURL = documentURL

        super.init()

        let previewQL = QLPreviewController()
        previewQL.dataSource = self as QLPreviewControllerDataSource
        previewQL.currentPreviewItemIndex = 0
        controller.showViewController(previewQL, sender: nil)
    }

    func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {
        return documentURL
    }
}

Any ideas why the refactored class PreviewController doesn't work?


Solution

  • I found this works

    import UIKit
    import QuickLook
    
    class PreviewController: QLPreviewController, QLPreviewControllerDataSource  {
    
        var documentURL: NSURL!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.dataSource = self
        }
    
        func show(controller: UIViewController, documentURL: NSURL) {
            self.documentURL = documentURL
    
            if let navController = controller.navigationController {
                navController.pushViewController(self, animated: true)
            } else {
                controller.showViewController(self, sender: nil)
            }
        }
    
        func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
            return 1
        }
    
        func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {
           return documentURL
        }
    
    }
    

    and calling like this

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let previewController = PreviewController()
        previewController.show(self, documentURL: urlList[indexPath.row])
    }