ios - 每次点击 collectionView 单元格时都会创建新视图,而不是更新现有视图
问题描述
我想在 collectionView Cells中有这个环形进度视图(可可豆荚) 。当您点击一个单元格时,进度应该增加 0.1(0.0 = 0% 进度,1.0 = 100% 进度)。
不幸的是,它似乎总是在旧的进度视图之上创建一个新的进度视图,因为红色的阴影越来越暗,你可以看到第二个/第三个/.. 环。我不知道如何解决这个问题。
这是我的代码:(Cell 类和 CollectionViewController 类)
import UIKit
private let reuseIdentifier = "Cell"
class myCollectionViewController: UICollectionViewController {
@IBOutlet var myCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
myCollectionView.reloadData()
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! myCollectionViewCell
if(indexPath.item != 0){
cell.setProgress(progress: 1 / Double(indexPath.item))
}
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! myCollectionViewCell
cell.setProgress(progress: cell.getProgress() + 0.1)
myCollectionView.reloadData()
}
}
import UIKit
import MKRingProgressView
class myCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var ringView: UIView!
let ringsize = 150
var ringProgressView = RingProgressView()
override func layoutSubviews() {
ringProgressView = RingProgressView(frame: CGRect(x: 0, y: 0, width: ringsize, height: ringsize))
ringProgressView.startColor = .red
ringProgressView.endColor = .magenta
ringProgressView.ringWidth = CGFloat(ringsize) * 0.15
ringProgressView.progress = 0.0
ringProgressView.shadowOpacity = 0.5
ringView.addSubview(ringProgressView)
}
}
extension myCollectionViewCell{
func setProgress(progress: Double){
UIView.animate(withDuration: 0.5){
self.ringProgressView.progress = progress
}
}
func getProgress() -> Double{
return self.ringProgressView.progress
}
}
这是启动时的样子(在 (1/indexpath.item) 处测试的进度:
这就是我点击底部左圆环 1 次时的样子:
我可以通过动画看到,第二个环以相同的进度覆盖左下圆圈的第一个环。不知何故,其他戒指也发生了变化……为什么?您甚至可以在其他一些圆圈上看到第二个环。您还可以看到红色的阴影变暗了一点。当我多次点击时,一切都变得完全(密集)红色。
谢谢你的帮助!
解决方案
简单用法:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! myCollectionViewCell
cell.setProgress(progress: cell.getProgress() + 0.1)
}
你不应该这样打电话:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! myCollectionViewCell
cell.setProgress(progress: cell.getProgress() + 0.1)
myCollectionView.reloadData()
}
因为cell重用机制,</p>
你打电话
collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! myCollectionViewCell
你得到一个对象池的单元格,即一个新创建的单元格,或者一个已创建但不可见的单元格
myCollectionView.reloadData()
更糟糕的是,打电话给这个,只是做重置,
因为你不维护数据。
推荐阅读
- django - How does Django knows which template to render when?
- docker - 在端口 443 上使用 ssl 的 Docker phoenix 没有收到任何内容
- sql - 更改加入日期格式为月-日,年sql
- php - 几个月的 PHP 日期时间差返回错误的值、错误的 php 配置或错误
- angular - Ionic App 在更新到 Angular 8 后中断 -> 无法解析 setupPlatform 的所有参数:(?, ...)
- python - Python:“系列”对象是可变的,因此它们不能被散列
- c# - 在 Xamarin.MAC 上使用 WebView 时出错
- .net - QuillJS 与 .net blazor 最大字符串长度
- indexing - Rust 无法索引类型为 'datatype<{integer}, {integer}>` 的值
- python - 如何使用 gitpython 删除特定分支