2016-03-08 49 views
0

我有這個類的正常工作QuickLook的類不工作

進口的UIKit 進口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! 
    } 
} 

但是當我改變快看看是另一個類,如下所示它停止工作?

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

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

任何想法,爲什麼重構類PreviewController不起作用?

+0

當我運行PreviewController我得到了一個空白頁面 – zackhalil

回答

1

我發現這個工作

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 
    } 

} 

,並呼籲像這樣

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