cellをタップした時にそのcellの色を変わるようにしたんですが、うまく行かないのですがなんででしょうか。また、どう直せばいいのか教えて欲しいです。
ViewControl.swift
1...省略... 2func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 3 let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CustomUICollectionViewCell 4 cell.image.backgroundColor = UIColor.red 5 print("Num: (indexPath.row)") 6 usePhoto = indexPath.row 7 8 9 10 11 } 12 13 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 14 return icon.count 15 } 16 17 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 18 let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! CustomUICollectionViewCell 19 cell.image.image = icon[indexPath.row] 20 return cell 21 }
CustomUICollectionViewCell.swift
1import UIKit 2 3class CustomUICollectionViewCell: UICollectionViewCell { 4 var image = UIImageView() 5 var myview = UIView() 6 required init(coder aDecoder: NSCoder) { 7 super.init(coder: aDecoder)! 8 } 9 10 override init(frame: CGRect) { 11 super.init(frame: frame) 12 13 14 image = UIImageView(frame: CGRect(x:0, y:0, width:frame.width, height:frame.height)) 15 myview = UIImageView(frame: CGRect(x:0, y:0, width:frame.width, height:frame.height)) 16 image.backgroundColor = UIColor.clear 17 //myview.backgroundColor = UIColor.red 18 self.contentView.addSubview(myview) 19 self.contentView.addSubview(image) 20 } 21 22 23}
「うまく行かない」とは?
色が変わらないです。
回答1件
あなたの回答
tips
プレビュー