I’ve set up 2 Collection Views in Xcode - one holds empty cells with background colours and the other holds a collection of images.
When I select a cell in the first collection view I want to change all the background colours in the second Collection View to match the background colour of the cell I’ve selected.
The problem I’ve got is that only the cell with the matching index path changes in the second Collection View ie if I select the 3rd colour, the 3rd image changes.
How do I make it so that all the cells in the second Collection View change colour? I know it’s something to do with the index path but can’t figure out how to get round it.
Code:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var colourView: UICollectionView!
@IBOutlet weak var iconView: UICollectionView!
@IBOutlet weak var height: NSLayoutConstraint!
let colours = [
UIColor(red: 0.94, green: 0.31, blue: 0.41, alpha: 1.00),
UIColor(red: 0.95, green: 0.45, blue: 0.23, alpha: 1.00),
UIColor(red: 1.00, green: 0.89, blue: 0.49, alpha: 1.00),
UIColor(red: 0.73, green: 0.72, blue: 0.42, alpha: 1.00),
UIColor(red: 0.01, green: 0.44, blue: 0.61, alpha: 1.00),
UIColor(red: 0.56, green: 0.89, blue: 0.90, alpha: 1.00),
UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.00),
]
let images = [UIImage(named: "home"), UIImage(named: "work"), UIImage(named: "key"), UIImage(named: "supermarket"), UIImage(named: "restaurant"), UIImage(named: "tree") , UIImage(named: "parking"), UIImage(named: "rugby"), UIImage(named: "medicine"), UIImage(named: "mortar"), UIImage(named: "ticket"), UIImage(named: "star")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (collectionView == iconView){
return self.images.count
}
return self.colours.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = view.frame.size.width
if (collectionView == iconView){
return CGSize(width: width * 0.2, height: width * 0.2)
}
return CGSize(width: width * 0.1, height: width * 0.1)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == iconView){
let cell2 = iconView.dequeueReusableCell(withReuseIdentifier: "iconcell", for: indexPath) as! IconCollectionViewCell
cell2.iconimage.image = self.images[indexPath.item]
return cell2
}
let colourcell = colourView.dequeueReusableCell(withReuseIdentifier: "colourcell", for: indexPath) as! ColourCollectionViewCell
colourcell.backgroundColor = colours[indexPath.row]
return colourcell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let cell = colourView.cellForItem(at: indexPath) as! ColourCollectionViewCell
let cell2 = iconView.cellForItem(at: indexPath) as! IconCollectionViewCell
if cell.isSelected {
cell2.iconimage.backgroundColor = cell.backgroundColor
}
}
}
Thanks in advance
Declare a var:
class ViewController:
var newColor : UIColor?
Change this:
if cell.isSelected {
cell2.iconimage.backgroundColor = cell.backgroundColor
}
for this:
if cell.isSelected {
newColor = cell.backgroundColor
iconView.reloadData()
}
in
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
put this
if newColor != nil { // maybe a better test here, if cell was selected
cell2.iconimage.backgroundColor = newColor
}
before
return cell2