UICollectionView reloadData 只能工作两次

Ano*_*ude 4 pdfkit ios uicollectionview swift

我有一个UIViewController显示一个UIDocumentPicker选择PDF文件的文件,并包含一个UICollectionView显示它们的文件(每个单元格都包含一个PDFView来执行此操作)。

这是代码:

import MobileCoreServices; import PDFKit; import UIKit

class ViewController: UIViewController {
    var urls: [URL] = []
    @IBOutlet weak var collectionView: UICollectionView!

    @IBAction func pickFile() {
        DispatchQueue.main.async {
            let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
            documentPicker.delegate = self
            documentPicker.modalPresentationStyle = .formSheet
            self.present(documentPicker, animated: true, completion: nil)
        }
    }
    
    override func viewDidLoad() {
        collectionView.register(UINib(nibName: PDFCollectionViewCell.identifier, bundle: .main),
                                forCellWithReuseIdentifier: PDFCollectionViewCell.identifier)
    }
    
    init() { super.init(nibName: "ViewController", bundle: .main) }
    required init?(coder: NSCoder) { fatalError() }
}

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PDFCollectionViewCell.identifier, for: indexPath) as! PDFCollectionViewCell
        cell.pdfView.document = PDFDocument(url: urls[indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        CGSize(width: 150, height: 150)
    }
}

extension ViewController: UIDocumentPickerDelegate {
    // MARK: PDF Picker Delegate
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        controller.dismiss(animated: true, completion: nil)
        
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        controller.dismiss(animated: true, completion: {
            DispatchQueue.main.async {
                self.urls.append(contentsOf: urls)
                self.collectionView.reloadData()
            }
        })
    }
}

class PDFCollectionViewCell: UICollectionViewCell {
    static let identifier = "PDFCollectionViewCell"
    
    @IBOutlet weak var pdfView: PDFView! { didSet { setPdfViewUI() } }
    
    func setPdfViewUI() {
        pdfView.displayMode = .singlePage
        pdfView.autoScales = true
        pdfView.displayDirection = .vertical
        pdfView.isUserInteractionEnabled = false
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,由于某种原因,collectionView.reloadData()实际上只有两次有效。它第一次工作,然后第二次没有任何反应,然后第三次集合视图再次使用三个预期元素更新......

我意识到,即使我正在调用reloadData(),当发生这种情况时,数据源和委托方法 ( numberOfItems/ cellForItem) 也不会被调用。

知道发生了什么吗?

感谢您的帮助!

viewDidLoad编辑:我可以确保/方法中没有任何其他代码appear,该pickFile函数实际上被正确调用,并且正确获取了 url,并且urls在调用 之前更新了数组reloadData()

另外,我已经尝试过使用UITableViewUICollectionView,并且在每种情况下都遇到这个问题。只是感觉好像我使用的事实有问题PDFView,或者文档选择器有问题。

Tar*_*agi 5

这是一个非常非常奇怪的错误,当您PDFViewUICollectionViewCell. 我在以下环境中证实了这一点 -

  1. Xcode 12.5
  2. iPhone SE 2020(iOS 14.6)

UICollectionView.reloadData()当添加为内部子视图时,调用无法可靠地工作PDFViewUICollectionViewCell.contentView

我们还能尝试什么?

令人惊讶的是,在这种情况下不适用的UICollectionView.insertItems(at:)地方却有效。UICollectionView.reloadData()本答案末尾提供了工作代码示例,供其他尝试重现/确认问题的人使用。

为什么会发生这种情况?

老实说不知道。UICollectionView.reloadData()应该保证 UI 与您的数据源同步。reloadData()让我们看一下(在本例中它起作用时) &的堆栈跟踪insertItems(at:)

重新加载数据_StackTrace

在此输入图像描述

InsertItemsAtIndexPaths_StackTrace

在此输入图像描述

结论

  1. reloadData()依赖于layoutSubviews()执行 UI 刷新。这是继承自UIViewlike - UIView.layoutSubviews() > UIScrollView > UICollectionView。这是一个非常著名的 UI 事件,可以很容易地被UIView. PDFView: UIView也可以这样做。为什么会出现不一致的情况?只有能够拆卸和检查的人才PDFKit.framework可能知道这一点。这显然是PDFView.layoutSubviews()实现中的一个错误,干扰了其超级视图的layoutSubviews()实现。

  2. insertItems(at:)在指定的索引路径处添加单元格的新实例,并且显然不依赖于此layoutSubviews(),因此在这种情况下可以可靠地工作。

示例代码

import MobileCoreServices
import PDFKit
import UIKit

class ViewController: UIViewController {
    
    // MARK: - Instance Variables
    private lazy var flowLayout: UICollectionViewFlowLayout = {
        let sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.sectionInset = sectionInset
        layout.itemSize = CGSize(width: 150, height: 150)
        layout.minimumInteritemSpacing = 20
        layout.minimumLineSpacing = 20
        
        return layout
    }()
    
    private lazy var pdfsCollectionView: UICollectionView = {
        let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
        cv.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        cv.backgroundColor = .red
        
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()
    
    private lazy var pickFileButton: UIButton = {
        let button = UIButton(frame: CGRect(x: 300, y: 610, width: 60, height: 40)) // hard-coded for iPhone SE
        button.setTitle("Pick", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .purple
        
        button.addTarget(self, action: #selector(pickFile), for: .touchUpInside)
        return button
    }()
    
    private var urls: [URL] = []
    
    
    // MARK: - View Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(pdfsCollectionView)
        pdfsCollectionView.register(
            PDFCollectionViewCell.self,
            forCellWithReuseIdentifier: PDFCollectionViewCell.cellIdentifier
        )
        
        self.view.addSubview(pickFileButton)
    }
    
    
    // MARK: - Helpers
    @objc private func pickFile() {
        let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
        documentPicker.delegate = self
        documentPicker.modalPresentationStyle = .formSheet
        self.present(documentPicker, animated: true, completion: nil)
    }
    
}

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PDFCollectionViewCell.cellIdentifier, for: indexPath) as! PDFCollectionViewCell
        cell.pdfView.document = PDFDocument(url: urls[indexPath.row])
        cell.contentView.backgroundColor = .yellow
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }
}

extension ViewController: UIDocumentPickerDelegate {
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        
        // CAUTION:
        // These urls are in the temporary directory - `.../tmp/<CFBundleIdentifier>-Inbox/<File_Name>.pdf`
        // You should move/copy these files to your app's document directory
        
        controller.dismiss(animated: true, completion: {
            DispatchQueue.main.async {
                let count = self.urls.count
                var indexPaths: [IndexPath] = []
                for i in 0..<urls.count {
                    indexPaths.append(IndexPath(item: count+i, section: 0))
                }
                
                self.urls.append(contentsOf: urls)
                
                // Does not work reliably
                /*
                self.pdfsCollectionView.reloadData()
                */
                
                // Works reliably
                self.pdfsCollectionView.insertItems(at: indexPaths)
            }
        })
    }
}


class PDFCollectionViewCell: UICollectionViewCell {
    static let cellIdentifier = "PDFCollectionViewCell"
    
    lazy var pdfView: PDFView = {
        let view = PDFView(frame: self.contentView.bounds)
        view.displayMode = .singlePage
        view.autoScales = true
        view.displayDirection = .vertical
        view.isUserInteractionEnabled = false
        
        self.contentView.addSubview(view)
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.backgroundColor = .yellow
        
        return view
    }()
}
Run Code Online (Sandbox Code Playgroud)

  • 很棒的调查!在我重现该问题后,我打算尝试插入,但由于某种原因从未解决过它 (2认同)