CollectionView Cell 中 Label 的值与旧值重叠一毫秒,而 reloadItems()
Value of a Label inside a CollectionView Cell is overlapping old value for a millisecond while reloadItems()
我已经针对此行为提出了问题。我想你明白我的意思,如果不明白请问。所以我有一个带有 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!
}
尝试添加一些动画,例如 loading
屏幕,这在用户界面方面看起来也更好。
使用像 MBProgressHUD
这样的第三方库,这样您的应用程序看起来会更干净,有关安装的详细信息,请查看 link。
添加 MBProgressHUD
后你的代码看起来像...
创建一个简单的可重用函数(在需要时随时调用它)
func needSomeAnimation(msg: String, mode: MBProgressHUDMode){
let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
loadingNotification.mode = mode
loadingNotification.label.text = msg
loadingNotification.hide(animated: true, afterDelay: 2.0)
}
如果您需要,现在就可以调用它。
@IBAction func touchButton(_ sender: Any) {
needSomeAnimation(msg: "Loading", mode: MBProgressHUDMode.indeterminate)
if datasource[0] == 1 {
datasource[0] = 3
} else {
datasource[0] = 1
}
cellectionview.reloadItems(at: [IndexPath(item: 0, section: 0)])
}
希望我的解决方案对你有用 : D
使用 reloadItems(at: [IndexPath])
,CollectionView 会使用默认动画 FadeInOut 自动更新值。您可以通过简单地调用函数 func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
并使用动画块并将时间设置为 0.0 来避免此动画。
UIView.animate(withDuration: 0) {
self.cellectionview.performBatchUpdates({
self.cellectionview.reloadItems(at: [IndexPath(item: 0, section: 0)])
}, completion: nil)
}
我已经针对此行为提出了问题。我想你明白我的意思,如果不明白请问。所以我有一个带有 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!
}
尝试添加一些动画,例如 loading
屏幕,这在用户界面方面看起来也更好。
使用像 MBProgressHUD
这样的第三方库,这样您的应用程序看起来会更干净,有关安装的详细信息,请查看 link。
添加 MBProgressHUD
后你的代码看起来像...
创建一个简单的可重用函数(在需要时随时调用它)
func needSomeAnimation(msg: String, mode: MBProgressHUDMode){
let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
loadingNotification.mode = mode
loadingNotification.label.text = msg
loadingNotification.hide(animated: true, afterDelay: 2.0)
}
如果您需要,现在就可以调用它。
@IBAction func touchButton(_ sender: Any) {
needSomeAnimation(msg: "Loading", mode: MBProgressHUDMode.indeterminate)
if datasource[0] == 1 {
datasource[0] = 3
} else {
datasource[0] = 1
}
cellectionview.reloadItems(at: [IndexPath(item: 0, section: 0)])
}
希望我的解决方案对你有用 : D
使用 reloadItems(at: [IndexPath])
,CollectionView 会使用默认动画 FadeInOut 自动更新值。您可以通过简单地调用函数 func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
并使用动画块并将时间设置为 0.0 来避免此动画。
UIView.animate(withDuration: 0) {
self.cellectionview.performBatchUpdates({
self.cellectionview.reloadItems(at: [IndexPath(item: 0, section: 0)])
}, completion: nil)
}