iosswiftqlpreviewcontroller

QLPreviewController not loading multiple files


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?


Solution

  • 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.