我已经问了一个问题,对于这种行为。我想你明白我的意思是,如果没有请询问。所以我有一个的CollectionView有点示范项目,并与它的整数数据源。因为较低的CPU占用率在我的项目,我必须通过调用reloadItems(:在[IndexPath] AT)刷新细胞。但是,当我刷新与reloadItems(在:在:[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.
}
}
很简单。当我触摸按钮的数据源变化的第一个值,我只需调用重载项目仅针对第一细胞的功能。
这是我的自定义单元格的代码:
class MyCell: UICollectionViewCell {
@IBOutlet weak var cellvalue: UILabel!
}
随着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)
}
尝试添加一些动画像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