- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经针对此行为提出了问题。我想你明白我的意思,如果不明白请问。所以我有一个带有 collectionView 的小演示项目和一个包含整数的数据源。在我的项目中,由于 cpu 使用率较低,我必须通过调用 reloadItems(at: at: [IndexPath]) 来刷新单元格。但是,当我使用 reloadItems(at: at: [IndexPath]) 刷新单元格时,我可以在一毫秒内看到单元格标签中的旧值。它看起来并不完美。
这是我来自 ViewController.swift 的代码
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return datasource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let mycell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCell
mycell.cellvalue.text = String(datasource[indexPath.row])
return mycell
}
@IBOutlet weak var cellectionview: UICollectionView!
@IBAction func touchButton(_ sender: Any) {
if datasource[0] == 1 {
datasource[0] = 3
} else {
datasource[0] = 1
}
cellectionview.reloadItems(at: [IndexPath(item: 0, section: 0)])
}
var datasource: [Int] = [1, 2, 3, 4, 5, 6]
override func viewDidLoad() {
super.viewDidLoad()
cellectionview.dataSource = self
cellectionview.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
很简单。当我触摸按钮时,数据源的第一个值发生变化,我只为第一个单元格调用重新加载项目函数。
这就是我的 CustomCell 的代码:
class MyCell: UICollectionViewCell {
@IBOutlet weak var cellvalue: UILabel!
最佳答案
使用 reloadItems(at: [IndexPath])
,CollectionView 使用默认动画 FadeInOut 自动更新值。您可以通过简单地调用函数 func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
并使用动画来避免此动画 block 并将时间设置为 0.0。
UIView.animate(withDuration: 0) {
self.cellectionview.performBatchUpdates({
self.cellectionview.reloadItems(at: [IndexPath(item: 0, section: 0)])
}, completion: nil)
}
关于swift - CollectionView Cell 中 Label 的值与旧值重叠一毫秒,而 reloadItems(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54474627/
我很难找到使用 NSDiffableDataSourceSnapshot reloadItems(_:) : 如果我要求重新加载的项目与数据源中已经存在的项目不相等,我会崩溃: Terminating
我想在用户选择联系人(方法didSelect contact)后仅重新加载单个单元格。如果单元格中的项目数量越来越大,该过程就会变得更慢。我还尝试在 DispatchQueue.main.async
我正在尝试创建一个程序,当用户在 UITextField 中输入文本时动态更新 collectoinView。下面是我为实现此目的而创建的扩展 我已经根据您的建议更新了代码以包含插入和删除语句,我仍在
我的单元格有一个从网络下载的图像,因此我需要将单元格的高度设置为动态。图片下载完成后,我将调用 self.collectionView.reloadItems(at: [indexPath]) 来触发
我有一个包含 uicollectionview 的 View Controller .每个collectionview 单元格都包含一个按钮,单击该按钮会在单元格中添加一个新标签。为了扩大每个单元格的
我有一个带有原型(prototype)单元格的 CollectionView。单元格有一个标签。随着时间的推移,标签经常更改值。我使用 collectionView.reloadItems[at: [
我有一个效果很好的 NSOutlineView。如果我更改模型,则当我调用:[self.myOutlineView reloadData] 时,大纲 View 会更新。然而,当我分析我的程序时,似乎有
我一直在使用 UICollectionView.reloadData() 来动态更新我的单元格,这非常有效,但它会导致动画非常笨拙。我连接到 func collectionView(_ collect
我已经针对此行为提出了问题。我想你明白我的意思,如果不明白请问。所以我有一个带有 collectionView 的小演示项目和一个包含整数的数据源。在我的项目中,由于 cpu 使用率较低,我必须通过调
我有一个 collectionView 单元格,它有一个 isExpanded: Bool 属性。当单元格第一次加载时,它被设置为 false 并且它只显示 1 行文本。如果文本有几行那么长,我会在第
我是一名优秀的程序员,十分优秀!