私はプログラムでコレクションビューを作成しています。viewDidLoad関数のコードは次のとおりです。
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
layout.itemSize = CGSize(width: 90, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView!.layer.cornerRadius = 2
collectionView!.backgroundColor = UIColor.redColor()
self.containerView.addSubview(collectionView!)
これらは私のコレクションビュー関数です
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.blackColor()
cell.buttonView.addTarget(self, action: Selector("Action\(indexPath.row):"), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
セルにボタンがあり、それを押すとセルの色が変わるというアイデアです。これがアクション関数です。
func Action0(sender: UIButton!) {
var indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath ) as! CollectionViewCell
cell.backgroundColor = UIColor.blueColor()
これはcollectionViewCellクラスです。
class CollectionViewCell: UICollectionViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var buttonView: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
buttonView = UIButton.buttonWithType(UIButtonType.System) as! UIButton
buttonView.frame = CGRect(x: 0, y: 16, width: frame.size.width, height: frame.size.height*2/3)
contentView.addSubview(buttonView)
}
}
ボタンとアクションは機能しますが、セルの色は変わりません。問題はこの行にあると思います。
let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath ) as! CollectionViewCell
ベストアンサー1
指定された indexPath のセルを取得する方法は次のとおりです。
let cell = collectionView!.cellForItemAtIndexPath(indexPath)
ただし、次のことも試してみるとよいでしょう:
cell.contentView.backgroundColor = UIColor.blueColor()
注記:
上記はうまくいったかもしれませんが、同じ機能を実現するために別の実装を試すことをお勧めします。実装では、CollectionViewCell ごとに個別の Action# 関数が必要になり、各メソッドで indexPath を手動で作成する必要がありますが、実際には 1 つしか作成できない可能性があります。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.blackColor()
cell.buttonView.addTarget(self, action: Selector("action"), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
そしてその関数その 1 つの方法は次のようになります。
func action(sender: UIButton!) {
var point : CGPoint = sender.convertPoint(CGPointZero, toView:collectionView)
var indexPath = collectionView!.indexPathForItemAtPoint(point)
let cell = collectionView!.cellForItemAtIndexPath(indexPath)
cell.backgroundColor = UIColor.blueColor()
}
Swift 4.2 のアクション機能を更新しました
@objc func action(sender: Any){
print("tickedOffPressed !")
if let button = sender as? UIButton {
let point: CGPoint = button.convert(.zero, to: collectionView)
if let indexPath = collectionView!.indexPathForItem(at: point) {
let cell = collectionView!.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.blue
}
}
}
単なる提案です! 楽しいコーディングを! :)