如何使用swift在UICollectionView中获取单元格的索引?
我正在编程集合视图。 这里是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!)
这些是我的collections查看function
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 }
想法是我在单元格中有一个button,当我按下它时,单元格应该改变颜色,这里是动作函数
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) }
}
button和动作的作品,但细胞不会改变颜色,我想这个问题是在这一行
let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath ) as! CollectionViewCell
这是在给定的indexPath处获取单元格的方法:
let cell = collectionView!.cellForItemAtIndexPath(indexPath)
不过,你也可以尝试:
cell.contentView.backgroundColor = UIColor.blueColor()
注意:
虽然上面的工作可能已经奏效,但我想鼓励您尝试不同的实现来实现相同的function。 您的实现将要求您为每个CollectionViewCell单独使用一个Action#函数,并且在每个方法中手动创buildindexPath,而您可能只有一个!
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 }
而且这个方法的function是这样的:
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() }
只是一个build议! 快乐的编码! 🙂
尝试cell.selected然后改变颜色
在Swift 2中,你可以得到这个行:
let point : CGPoint = sender.convertPoint(CGPointZero, toView:myTableView) let indexPath = myTableView.indexPathForRowAtPoint(point) let cell = myTableView.cellForRowAtIndexPath(indexPath!)