Ios 我应该在UICollectionViewCell的哪个方法中编写只运行一次的代码?

Ios 我应该在UICollectionViewCell的哪个方法中编写只运行一次的代码?,ios,swift,swift2,uicollectionviewcell,Ios,Swift,Swift2,Uicollectionviewcell,我希望在UICollectionView设置其imageView.image后修改其布局 我想知道对我的可重用单元只执行一次代码的正确方法 到目前为止,对我有效的方法是在cellForRow()方法中在其中绘制自定义视图,但这会使代码变得混乱。我想在cell类中执行代码 Init(带框架)不起作用。它甚至没有从我的collectionView调用 此外,如果有人能给我一个关于UICollectionViewCell最重要的方法是什么以及何时使用它们的快速解释,我将不胜感激 编辑: 这是我要从ce

我希望在
UICollectionView
设置其
imageView.image
后修改其布局

我想知道对我的可重用单元只执行一次代码的正确方法

到目前为止,对我有效的方法是在
cellForRow()
方法中在其中绘制自定义视图,但这会使代码变得混乱。我想在cell类中执行代码

Init
(带框架)不起作用。它甚至没有从我的
collectionView
调用

此外,如果有人能给我一个关于
UICollectionViewCell
最重要的方法是什么以及何时使用它们的快速解释,我将不胜感激

编辑:

这是我要从
cellForRow()
中删除并移动到单元格类中的代码:

firstProductCell.imageView.image = UIImage(named: "photo1")
firstProductCell.textLabel.text = "Featured title"

let imgview: UIImageView = UIImageView(frame: CGRectMake(0, 0, 1100, 600))
imgview.image = firstProductCell.imageView.image

let whiteView: UIView = UIView(frame: CGRectMake(1200, 0, 600, 600))
whiteView.backgroundColor = UIColor.whiteColor()

let view: UIView = UIView(frame: cell.bounds)
view.addSubview(imgview)
view.addSubview(whiteView)

UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
firstProductCell.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

不幸的是,我不知道如何创建单元格,但是: 如果将自定义单元格类注册到collectionView:

    collection.registerClass(CustomCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "CellIdentifier")
然后在
单元ForItemAtIndexPath
中使用:

let cell : CustomCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CellIdentifier", forIndexPath: indexPath) as! CustomCollectionViewCell
然后将调用CustomCollectionViewCell的init with frame方法:

override init(frame: CGRect)
    {
        super.init(frame: frame)
    }
awakeFromNib()-


awakeFromNib
?最好在问题中显示单次运行的代码,因为它在单元格中实际上可能不合适……除最后4行之外的所有内容似乎都可以使用interface builder创建,无需代码。为什么要让单元格自己填充数据,这是cellForRow方法的工作。我没有将
whiteView
imgView
添加到单元格(将在Interface Builder中创建)。我创建它们是为了创建两者的组合视图,并将其添加为图像。将其添加为图像非常重要,因为它受益于tvOS上的特殊动画。我不希望在Interface Builder中创建它们。
class YourCollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

       //do some work here that needs to happen only once, you don’t wanna change them later.
    }
}