UICollectionView reloadData 只工作两次

如何解决UICollectionView reloadData 只工作两次

我有一个 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
    }
}

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

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

知道发生了什么吗?

感谢您的帮助!

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

此外,我在 UITableViewUICollectionView 上都尝试过这种方法,并且在每种情况下都遇到了这个问题。感觉好像是我使用了 PDFView 或文档选择器有问题。

解决方法

这是一个非常非常奇怪的错误,当您在 PDFView 中使用 UICollectionViewCell 时会发生这种错误。我在以下环境中确认了这一点 -

  1. Xcode 12.5
  2. iPhone SE 2020 (iOS 14.6)
UICollectionView.reloadData() 作为子视图添加到 PDFView 中时,

UICollectionViewCell.contentView 调用不可靠

我们还能尝试什么?

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

为什么会发生这种情况?

老实说不知道。 UICollectionView.reloadData() 应该保证 UI 与您的数据源同步。让我们看看 reloadData()(在这种情况下工作时)和 insertItems(at:) 的堆栈跟踪。

ReloadData_StackTrace

enter image description here

InsertItemsAtIndexPaths_StackTrace

enter image description here

结论

  1. reloadData() 依赖于 layoutSubviews() 来执行 UI 刷新。这是从 UIView 继承的,例如 - UIView.layoutSubviews() > UIScrollView > UICollectionView。这是一个众所周知的 UI 事件,很容易被 UIView 的任何子类拦截。 PDFView: UIView 也可以这样做。 为什么会出现不一致的情况?只有能够拆卸和检查 PDFKit.framework 的人才可能知道这一点。这显然是 PDFView.layoutSubviews() 实现中的一个错误,干扰了它的超级视图的 layoutSubviews() 实现。

  2. insertItems(at:) 在指定的 indexPath(s) 添加单元格的新实例,并且显然不依赖于 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
    }()
}
,

这是由您的单元格中的 PDFViews 引起的,原因我不确定。 PDFView 对于在集合视图单元格中使用来说可能过于重量级,或者在重新加载或重新添加文档时可能会出现一些问题。我在没有设法修复它的情况下重现了您的问题,但您可能需要牢记以下几点:

  • 您正在为文档选择器使用已弃用的初始化程序
  • 不能保证您获得的“收件箱”网址是持久的,实际上,您可以在选择更多文档时看到文件在其中出现和消失,因此您可能希望考虑将所选文件移至更永久的主页

解决这些问题对奇怪的重新加载问题没有影响。我所做的是 import QuickLookThumbnailing,向单元格添加图像视图而不是 PDF 视图,并添加以下代码:

class PDFCollectionViewCell: UICollectionViewCell {
    static let identifier = "PDFCollectionViewCell"
    
    @IBOutlet var imageView: UIImageView!
    private var request: QLThumbnailGenerator.Request?
        
    func load(_ url: URL) {
        let req = QLThumbnailGenerator.Request.init(fileAt: url,size: bounds.size,scale: UIScreen.main.scale,representationTypes: .all)
        
        QLThumbnailGenerator.shared.generateRepresentations(for: req) {
            [weak self]
            rep,type,error in
            DispatchQueue.main.async {
                self?.imageView.image = rep?.uiImage
            }
        }
        request = req
    }
    
    override func prepareForReuse() {
        if let request = request {
            QLThumbnailGenerator.shared.cancel(request)
            self.request = nil
        }
        imageView.image = nil
    }
}

这会异步呈现您传递给它的任何 URL 的缩略图,从而提高质量。

,

以某种方式 collectionView 缓存了 reloadingData 毫秒,因此当您调用 reloadData() 两次时它不会重新加载两次,同样基于 apple developer documentation

您不应在插入或删除项目的动画块中间调用此方法。插入和删除会自动导致集合的数据得到适当更新。

我有一些解决方法来实现这一点:

使 collectionView 布局无效:

据我所知,collectionViewLayout prepare 里面有 remove cache 方法

collectionView!.reloadData()
collectionView!.collectionViewLayout.invalidateLayout()
collectionView!.layoutSubviews()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-