나는 UICollectionViewCell 있습니다실패는
"그라데이션보기"그라데이션이있는 UIView의 수 있습니다 : 데이터의
원본입니다 CoreData. 컬렉션 뷰에 는, 그 버튼 종류 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()
}
정렬 버튼 콜렉션 뷰 다시로드를 누른 후를 오른쪽 데이터를 표시합니다. 그러나 셀이 소멸되지 않은 것처럼 그라데이션보기가 곱합니다.
어떻게 내 휴대가 처음부터 다시 그려되어 있는지 확인 : 여기에 내가 여러 번 데이터를 정렬 한 후 무엇을보고 ?
편집 :
내 사용자 정의 셀 클래스입니다. 그럼 configureCell에서 그라디언트 뷰가보기에 추가되었는지 확인해야합니다. 그렇다면 그라디언트를 추가하고, 추가하지 않으면 추가하고 그라디언트를 추가해야합니다. 어떻게 수표를 발행 할 수 있습니까?
클래스 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)
}
}
편집 : 나는 확장자를 떨어 플래그 로직을 구현했습니다. 그러나보기를 제거한 후에는 검색을 수행 한 후에 셀에 절대 다시 나타나지 않습니다. 의견이 있으십니까? UICollectionViewCell
서브 클래스 (셀)에서
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)
}
}
시도하기 전에를 작성하고
Cell
셀 클래스로 인터페이스 빌더에서 지정 (물론 콘센트에 대해 잊지 마세요) -당신은
Cell
에 대한 사용자 정의 클래스가없는 경우 prepareForReuse 메서드 –@WetSweater 내부에서 수행해야 할 작업은 무엇입니까? –
셀 또는 collectionView 데이터 소스 메서드의 어딘가에 "addSubview"메서드를 사용합니까? –