如何使用swift获取UICollectionView中的单元格atIndex? [英] How to get cell atIndex in UICollectionView with swift?

查看:86
本文介绍了如何使用swift获取UICollectionView中的单元格atIndex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式创建Collection视图。
这是viewDidLoad func中的代码

I am making Collection view programatically. Here is the code in viewDidLoad func

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!)

这些是我的集合视图函数

And these are my collection view functions

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
}

想法是单元格中有一个按钮,当我按下它时,单元格应该改变颜色,这里是动作函数

Idea is I have a button in the cell, and when I press it the cell should change color, here is the action function

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类,

and this is collectionViewCell class,

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)
}

}

按钮和动作有效,但单元格没有改变颜色,我想问题就在这一行

The button and the action works but the cell doesn't change colour, i guess the problem is in this line

let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath ) as! CollectionViewCell


推荐答案

这是获取单元格的方法给定indexPath:

This is the way to get the cell at a given indexPath:

let cell = collectionView!.cellForItemAtIndexPath(indexPath)

但是,您也可以尝试:

cell.contentView.backgroundColor = UIColor.blueColor()

注意:

虽然以上内容可能有效,但我想鼓励您尝试不同的实现来实现相同的功能。您的实现将要求您为每个CollectionViewCell分别设置一个Action#函数,并在每个方法中手动创建indexPath,此时您可能只有一个!

Though the above may have worked, I want to encourage you to try a different implementation to achieve the same functionality. Your implementation would require you to have a separate Action# function for each CollectionViewCell, as well as create the indexPath manually in each of those methods, when you could potentially only have one!

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
}

并且该方法的函数类似于:

and the function for that one method would be something like:

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()
}

只是一个建议!快乐的编码! :)

Just a suggestion! Happy coding! :)

这篇关于如何使用swift获取UICollectionView中的单元格atIndex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆