本文介绍了QuickLook类无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这门课,效果很好
导入UIKit导入QuickLook
import UIKitimport 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])
}
}
和
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不起作用?
Any ideas why the refactored class PreviewController doesn't work?
推荐答案
我发现这可行
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])
}
这篇关于QuickLook类无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!