QuickLook class not working

六月ゝ 毕业季﹏ 提交于 2019-12-23 04:47:23

问题


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?


回答1:


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])
}


来源:https://stackoverflow.com/questions/35857291/quicklook-class-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!