问题描述
我已经在Xcode中设置了2个“收藏夹视图”-一个包含具有背景色的空单元格,另一个包含一组图像。
当我在第一个集合视图中选择一个单元格时,我想更改第二个集合视图中的所有背景色以匹配我选择的单元格的背景色。
我遇到的问题是,只有具有匹配索引路径的单元格才会在第二个“收藏夹视图”中发生变化,即,如果我选择了第三种颜色,那么第三幅图像就会发生变化。
如何使第二个“收藏夹视图”中的所有单元格都改变颜色?我知道这与索引路径有关,但无法弄清楚如何绕过它。
代码:
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,UIColor(red: 1.00,green: 0.89,blue: 0.49,UIColor(red: 0.73,green: 0.72,blue: 0.42,UIColor(red: 0.01,green: 0.44,blue: 0.61,UIColor(red: 0.56,blue: 0.90,UIColor(red: 0.84,green: 0.84,blue: 0.84,]
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
}
}
}
预先感谢
解决方法
声明一个变量:
class ViewController:
var newColor : UIColor?
更改此:
if cell.isSelected {
cell2.iconimage.backgroundColor = cell.backgroundColor
}
为此:
if cell.isSelected {
newColor = cell.backgroundColor
iconView.reloadData()
}
在
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
放入
if newColor != nil { // maybe a better test here,if cell was selected
cell2.iconimage.backgroundColor = newColor
}
之前
return cell2