-
-
Save MickaelCruzDB/8bcd0c4c7bd29206f0dac6b2af2ad711 to your computer and use it in GitHub Desktop.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
// Working with functions categories collection view | |
if collectionView == self.functionsCategoriesCollectionView { | |
let cellFunctionsCategories = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCat", for: indexPath) as! DevicePageFunctionsCategoriesCVCell | |
cellFunctionsCategories.cellTitleContent.text = ICDatabase.objects(FunctionCategory.self)[indexPath.row].name.uppercased() | |
cellFunctionsCategories.setGradientBorder(width: 10, colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0), UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0)]) | |
if indexPath == cellSelectionIndexPath { | |
if let cellToDeselect = collectionView.cellForItem(at: self.cellSelectionIndexPath) as? DevicePageFunctionsCategoriesCVCell { | |
cellToDeselect.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.clear.cgColor], angle: 0) | |
// Get all functions | |
self.filtered = GlobalVariables.currentProduct.functions.filter { _ in | |
return true | |
} | |
self.functionsCollectionView.reloadData() | |
} | |
} | |
return cellFunctionsCategories | |
} | |
} | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
// Working with functions categories collection view | |
if collectionView == self.functionsCategoriesCollectionView { | |
let cell = collectionView.cellForItem(at: indexPath) as! DevicePageFunctionsCategoriesCVCell | |
cell.isHidden = false | |
cell.cellView.isHidden = false | |
cell.isSelected = true | |
cell.cellView.clipsToBounds = true | |
cell.cellView.layer.cornerRadius = 25 | |
cell.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45) | |
if cellSelectionIndexPath == indexPath { | |
// it was already selected | |
cellSelectionIndexPath = nil | |
collectionView.deselectItem(at: indexPath, animated: true) | |
cell.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.clear.cgColor], angle: 0) | |
self.filtered = GlobalVariables.currentProduct.functions.filter { _ in | |
return true | |
} | |
self.functionsCollectionView.reloadData() | |
} else { | |
// wasn't yet selected, so let's remember it | |
cellSelectionIndexPath = indexPath | |
// Filter with seletec category name | |
let cellCategoryName = ICDatabase.objects(FunctionCategory.self)[indexPath.row] | |
self.filtered = GlobalVariables.currentProduct.functions.filter { function in | |
return function.functionCategory.contains(cellCategoryName) | |
} | |
self.functionsCollectionView.reloadData() | |
} | |
} | |
} |
didSelect method is just to capture selectedIndexPath and call reloadData. Don't update cell there directly. Follow my code added in stack overflow, those 2 lines are enough for this requirement.
Thanks a lot for your support I try this now
I edited my code as below. But I do not understand what I'm making wrong :
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Working with functions categories collection view
if collectionView == self.functionsCategoriesCollectionView {
let cellFunctionsCategories = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCat", for: indexPath) as! DevicePageFunctionsCategoriesCVCell
cellFunctionsCategories.cellTitleContent.text = ICDatabase.objects(FunctionCategory.self)[indexPath.row].name.uppercased()
cellFunctionsCategories.setGradientBorder(width: 10, colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0), UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0)])
//when cell selected add gradient to fill it
if indexPath == cellSelectionIndexPath {
cellFunctionsCategories.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45)
}
return cellFunctionsCategories
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Working with functions categories collection view
if collectionView == self.functionsCategoriesCollectionView {
if cellSelectionIndexPath == indexPath {
// it was already selected
cellSelectionIndexPath = nil
collectionView.deselectItem(at: indexPath, animated: true)
let selectedItems = collectionView.indexPathsForVisibleItems
for indexPath in selectedItems {
collectionView.deselectItem(at: indexPath, animated:true)
if let cell = collectionView.cellForItem(at: indexPath) as? DevicePageFunctionsCategoriesCVCell {
cell.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.clear.cgColor], angle: 0)
}
}
self.filtered = GlobalVariables.currentProduct.functions.filter { _ in
return true
}
self.functionsCollectionView.reloadData()
} else {
// wasn't yet selected, so let's remember it
cellSelectionIndexPath = indexPath
if let cell = collectionView.cellForItem(at: indexPath) as? DevicePageFunctionsCategoriesCVCell {
cell.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45)
}
// Filter with seletec category name
let cellCategoryName = ICDatabase.objects(FunctionCategory.self)[indexPath.row]
self.filtered = GlobalVariables.currentProduct.functions.filter { function in
return function.functionCategory.contains(cellCategoryName)
}
self.functionsCollectionView.reloadData()
}
}
Replace didSelectItem method with below code only. This will store selected indexPath, changing color is handled by cellForRowAtIndex method. You just need to reload collectionView.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Working with functions categories collection view
if collectionView == self.functionsCategoriesCollectionView {
cellSelectionIndexPath = indexPath
collectionView.reloadData()
}
}
I edited my code like you proposed. Here is the result : http://digitalblend.fr/BUG.mov I do not understand why all cells background change...
-
cellFunctionsCategories.setGradientBorder(width: 10, colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0), UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0)])
-
cellFunctionsCategories.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45)
Does these two methods does same action with different color?
The first add a gradient color only for the cell's border and second one a gradient color for the cell's background
Put this in CellForRowAtIndex method
// you have to set default color ( this must be your deselected color)
cellFunctionsCategories.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.white.cgColor], angle: 45)
//when cell selected add gradient to fill it
if indexPath == cellSelectionIndexPath {
// (this must be your selected color)
cellFunctionsCategories.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45)
}
I have exactly the same problem with :
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Working with functions categories collection view
if collectionView == self.functionsCategoriesCollectionView {
cellSelectionIndexPath = indexPath
let cellFunctionsCategories = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCat", for: indexPath) as! DevicePageFunctionsCategoriesCVCell
cellFunctionsCategories.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.white.cgColor], angle: 45)
//when cell selected add gradient to fill it
if indexPath == cellSelectionIndexPath {
// (this must be your selected color)
cellFunctionsCategories.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45)
}
collectionView.reloadData()
}
}
Code mentioned above is bad approach. You should not call reloadData inside cellforRowAt method. Inside if block, if condition is true, update same cell as shown below.