gpt4 book ai didi

ios - reloadData()后无法重绘UICollectionViewCell

转载 作者:行者123 更新时间:2023-11-30 12:43:32 39 4
gpt4 key购买 nike

我有一个 UICollectionViewCell:

enter image description here

“渐变 View ”是具有渐变的 UIView:

enter image description here

数据来源是CoreData。在 Collection View 上,我有对 CoreData 进行排序的按钮:

@IBAction func allMealsBtn(_ sender: Any) {
let fetchRequest: NSFetchRequest<Meal> = Meal.fetchRequest()
let dateSort = NSSortDescriptor(key: "created", ascending: false)
fetchRequest.sortDescriptors = [dateSort]

let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

controller.delegate = self
self.controller = controller

do {
try controller.performFetch()
} catch {
let error = error as NSError
print("Couldn't fetch data from CoreData with error: \(error.debugDescription)")
}
collectionView.reloadData()

}

按下排序按钮后, Collection View 将重新加载并显示正确的数据。然而,渐变 View 会成倍增加,就好像细胞没有被破坏一样。这是我多次对数据进行排序后看到的结果:

enter image description here

如何确保我的单元格从头开始重绘?

编辑:

我的自定义单元格类。所以在configureCell中我必须检查我的gradientView是否添加到 View 中,如果是,则添加渐变,如果没有,则添加它,然后添加渐变。我怎样才能进行这项检查?

类 CollectionCell:UICollectionViewCell {

@IBOutlet weak var mealImg: UIImageView!
@IBOutlet weak var mealTitleLbl: UILabel!
@IBOutlet weak var gradientView: UIView!

func configureCell(meal: Meal) {
mealTitleLbl.text = meal.title

let img = meal.getMealImage()
mealImg.image = img

gradientView.addGradientWithColor(color: UIColor.clear)

// How can I check if my gradientView is added to the view, if yes, then add gradient, if not, add it, and only then add gradient.

}

override func prepareForReuse() {
super.prepareForReuse()

gradientView.removeFromSuperview()
}
}

extension UIView {
func addGradientWithColor(color: UIColor) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
let topColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1)
gradient.colors = [topColor.cgColor, color.cgColor]

self.layer.insertSublayer(gradient, at: 0)
}
}

编辑:我已经放弃了扩展并实现了标志逻辑。但是, View 被删除后,执行搜索后它再也不会出现在单元格上。有什么想法吗?

class CollectionCell: UICollectionViewCell {

@IBOutlet weak var mealImg: UIImageView!
@IBOutlet weak var mealTitleLbl: UILabel!
@IBOutlet weak var gradientView: UIView!

var isGradientAdded = false

func configureCell(meal: Meal) {
mealTitleLbl.text = meal.title

let img = meal.getMealImage()
mealImg.image = img

if isGradientAdded == false {
addGradient()
isGradientAdded = true
}
}

override func prepareForReuse() {
super.prepareForReuse()

gradientView.removeFromSuperview()
}

func addGradient () {
let gradient = CAGradientLayer()
gradient.frame = gradientView.bounds
let topColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1)
let botomColor = UIColor.clear
gradient.colors = [topColor.cgColor, botomColor.cgColor]
gradientView.layer.insertSublayer(gradient, at: 0)
}
}

最佳答案

UICollectionViewCell 子类 (Cell) 中,您应该重写 prepeareForReuse 函数 - 将单元格转换为默认模式。

如果您没有 Cell 的自定义类 - 请先创建它,然后在界面生成器中将其指定为 Cell 单元格的类(当然,不要忘记销售点)

override func prepareForReuse() {
super.prepareForReuse()

//set cell to initial state here
//e.g try to remove you gradient view
yourGradientView.removeFromSuperview()
}

关于ios - reloadData()后无法重绘UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945974/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com