- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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()
实际上只工作两次。它第一次工作,然后第二次没有任何 react ,然后第三次使用三个预期元素再次更新集合 View ......
reloadData()
,发生这种情况时,不会调用 dataSource 和委托(delegate)方法 (
numberOfItems
/
cellForItem
)。
viewDidLoad
中没有任何其他代码/
appear
方法,即
pickFile
函数实际上被调用得很好,并且 url 被正确获取,使用
urls
在调用
reloadData()
之前按原样更新数组.
UITableView
上都试过了。和
UICollectionView
,而且我在每种情况下都遇到了这个问题。我正在使用
PDFView
的事实感觉有点问题。 ,或使用文档选择器。
最佳答案
这是一个非常非常奇怪的错误,当您使用 PDFView
时会发生这种错误。在 UICollectionViewCell
.我在以下环境中确认了这一点 -
UICollectionView.reloadData()
电话是
工作不可靠 当
PDFView
添加为
UICollectionViewCell.contentView
内的 subview .
UICollectionView.insertItems(at:)
工作地点
UICollectionView.reloadData()
不适用于这种情况。在此答案的末尾提供了工作代码示例,供其他任何试图重现/确认问题的人使用。
UICollectionView.reloadData()
应该保证 UI 与您的数据源同步。让我们看看
reloadData()
的堆栈跟踪(当它在这种情况下工作时)&
insertItems(at:)
.
reloadData()
依赖 layoutSubviews()
执行 UI 刷新。这是从 UIView
继承的喜欢 - UIView.layoutSubviews() > UIScrollView > UICollectionView
.这是一个非常有名的 UI 事件,很容易被 UIView
的任何子类拦截。 . PDFView: UIView
也可以这样做。 为什么会出现不一致的情况? 只有可以拆卸和检查 PDFKit.framework
的人可能知道这件事。这显然是 PDFView.layoutSubviews()
中的一个错误干扰它的 super View 的实现 layoutSubviews()
执行。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
}()
}
关于ios - UICollectionView reloadData 只工作两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67988863/
我正在制作一个应用程序,使用 Flickr 和 Imgur 的 API 提取图像。填充存储图像 URL 和图像标题的模型后,我想重新加载 UI,以便图像填充 collectionview,但是当它被调
出于某种原因[tView reloadData] (其中 tView 是 UITableView)不会刷新我的 UITableView。 cellForRowAtIndexPath , numberO
我正在更新我的表数据资源,然后我打电话 [self.tableView reloadData] 用这个新数据加载 TableView 。这一切都发生在一个线程中。现在,它可以正常工作,假设我添加了 4
我有一个 UICollectionViewCell: “渐变 View ”是具有渐变的 UIView: 数据来源是CoreData。在 Collection View 上,我有对 CoreData 进
我是一名新手 iOS 程序员,正在开发我的第一个应用程序,但遇到了一个小问题。我想在通过网络下载一些数据后更新我的 tableView,并在其 Controller 中使用以下代码来执行此操作: -(
我的应用程序包含两个 collectionView , mainCollectionView 和 nestedCollectionView ,位于 mainCollectionViewCell 中。
我是整个 iOS 开发的新手,所以不是很擅长。我目前有一个带有两个 TableView 的 viewController。第一个表始终可见并包含类别。只有在选择第一个表中的单元格时,第二个表才应该可见
我明白我需要在调用reloadData之前更改数据源中的数据。我的问题是我不确定这是如何完成的以及为什么我的 getData 方法不会覆盖当前单元格。是否有必要为此使用 subview ?或者有没有办
我有一个 4 行的分组 tableView。我对我在两个 View 之间做事的方式进行了大修,现在 reloadData 表现得很奇怪。它只会刷新一行中的数据。 (最后一个)和其他人都没有。 我已经在
当我实例化第三个单元格时,我会将更多项目添加到我的模型数组中,然后我将更新 Collection View 数据: DispatchQueue.main.async(execute: { s
所以,我遇到了麻烦 - 无法重新加载 TableView 的数据,在命令行中我看到了良好的数据 - 当我单击按钮时 idMeet 已更改,但在模拟器中我没有看到刷新 TableView 单击按钮后,i
我正在尝试从 firebase 获取数据并传递给 tableview。 // Model import UIKit import Firebase
我有 2 个类,classA 和 classB在 classA 中,我有一个可以按需工作和刷新的 TableView 。所有的委托(delegate)和数据源都很好,还有一个属性 @property
所以看起来 Swift 中的 TableView 会根据滚动位置动态地将数据重新加载到屏幕上。但是,在我的例子中,我不希望该机制以这种方式工作,因为我的 TableView 中的项目会更新用户订单的总
所以我有一个 tableView,其中有一个标题元素和一些单元格。我正在从服务器中提取单元格,然后在完成后重新加载 tableview。好吧,这至少是现在的目标我在每次追加到数组后重新加载 tavle
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我想知道 tableView 上的 reloadData 有多贵。我想每 5 秒刷新一次表格 View 。这会导致任何性能问题吗? 最佳答案 重新加载数据将调用 TableView 数据源上的两个主要
在我的应用程序中,我有一个标签,通知用户缺少一些设置: 设置在: override func tableView(_ tableView: UITableView, cellForRowAt inde
在 UITableView 上调用 reloadData 时实际调用了哪些方法? [tableView reloadData]; 最佳答案 整个 TableView 被重新加载,它调用 cellFor
我遇到这样一种情况,代码更改其 collectionView 数据源,然后多次调用 reloadData(),我认为这会导致我的应用程序因竞争条件而崩溃,因为数据源变化非常快,所以我用 Dispatc
我是一名优秀的程序员,十分优秀!