ios - reloadData()后无法重绘UICollectionViewCell

标签 ios swift core-data uicollectionview uicollectionviewcell

我有一个 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/

相关文章:

ios - 以编程方式触发呼吸应用程序或 HRV 测量 [Apple Watch 4]

ios - 解析 GeoPoint 限制

c - Swift3:将原始指针转换为 Int(指针本身就是 int 值,不指向任何地方!)

ios - 在 UITextView 上设置 UITextContentType.emailAddress 不起作用

objective-c - 持久存储迁移失败缺少源托管对象模型

core-data - 允许 ComplicationController 访问 CoreData SwiftUI

ios - 如何更新蓝牙设备的 periphera.name?

ios - 如何在 iOS 上获取内部名称?

ios - iOS 中使用 Swift 3 的 CollectionView 动态高度

iPhone 核心数据 - 导入附加内容