0

I've embedded a QLPreviewController as a sub view controller in a view controller of mine and have setup the delegate and data source for it:

qlPreviewController.delegate = self
qlPreviewController.dataSource = self

In my viewDidLoad, I'm downloading a remote document. Once it's finished downloading, I reload the QLPreviewController to display that document. This works fine.
Now, I want to show multiple documents at the same time. Therefor, I download multiple documents, setup an array of those and then reload the QLPreviewController.

Here's the implemented data source methods:

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    print("count:", downloadedDocuments.count)
    return downloadedDocuments.count
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    print("\(index+1)/\(downloadedDocuments.count), id:", downloadedDocuments[index].id)
    return downloadedDocuments[index].localURL as QLPreviewItem
}

What ends up happening is that only one document is being display, which is the first in the array. I added the print statement above, to verify that there are multiple documents in the downloadedDocuments array. It prints:

count: 5
1/5, id: 251

As you can see, it's calling previewItemAtIndex only once, and the index remains 0.
Why is that? Do I need to setup anything else?

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174

2 Answers2

1

Your whole title, about “loading multiple files”, suggests a misconception. I think you’re assuming that a QLPreviewController is a kind of collection view controller that displays multiple objects simultaneously. It isn’t. It previews one document. If it has multiple preview items, the user can page sideways to another document, or choose from the TOC menu, but it’s still showing just one document at a time.

Also, you seem to be trying to use the preview controller as an embedded. child view controller. You can’t. It can be presented, or pushed onto a navigation controller; that’s all. Don’t try to retain or reuse it. Don’t use its view as a subview. When it’s time to preview, create the controller and present or push it. When the controller is dismissed or popped, let it die.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Okay. In `numberOfPreviewItems` I need to pass all the files (in the documents folder in my case), right? Then that'd be the *items*, if I'm not mistaken. If not, then what is an *item*? – LinusGeffarth Oct 14 '18 at 10:09
  • Is the problem that you’ve never even seen a preview controller with multiple items, so you don’t know what to expect? – matt Oct 14 '18 at 10:13
  • Uhh maybe, yeah. I thought that in the side bar, you could see not only pages of the current file but also the next files. I just checked the behavior in the Files app and realized the side scrolling. Does that mean in a folder with 5 documents, Apple is opening a QLPreviewController and passing those there and then it handles the scrolling (left & right) automatically? – LinusGeffarth Oct 14 '18 at 10:31
  • Oh gosh, it actually works in my implementation, I just tried to get that side bar and scroll vertically. I can actually scroll sideways. Thanks for clearing this up! – LinusGeffarth Oct 14 '18 at 10:36
  • @matt I'm curious to hear your reasoning behind why the QLPreviewController should not be embedded as a child view controller. Since we cannot overlay QLPreviewController with custom controls, having it embedded as a child view controller would allow us to have a single instance of it and control what document it currently previews from "outside", using our custom UI. – damirstuhec Dec 12 '19 at 09:00
  • @matt What alternative should we use if we want QLPreviewController like features and it should work as a collection view with hundreds or thousands of items? Currently, it's not efficient at all. – Master AgentX Nov 25 '20 at 20:54
0

Turns out, I misunderstood how multiple items are loaded in a QLPreviewController.
I thought, the user had to scroll vertically and see the side bar with thumbnails. This is only for pages within a single document, however.

I had to scroll horizontally.
Thanks to @matt, who cleared this up for me.

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174