Ios 如何在CollectionView单元格中仅为一个indexPath.row项设置动画?

Ios 如何在CollectionView单元格中仅为一个indexPath.row项设置动画?,ios,swift,uicollectionview,nsindexpath,Ios,Swift,Uicollectionview,Nsindexpath,我有UICollectionView,我只想为一个单元格启动图像动画。但问题是,当我添加此动画时,会影响多个项目,而不是一个单元格。 代码如下: func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection

我有UICollectionView,我只想为一个单元格启动图像动画。但问题是,当我添加此动画时,会影响多个项目,而不是一个单元格。 代码如下:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return arrays.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell

    if (indexPath.row == 0)
    {
        //do the animation

问题是这些电池正在被重复使用。这样想,如果您有100个不同的对象,但一次只显示2个,那么collectionview将只创建(比如)大约4个单元格。它这样做是为了节省内存和提高性能

因此,如果屏幕上的单元格A和B分别显示了数组[0]和数组[1]的详细信息,然后开始滚动,那么接下来将出现的单元格将是显示数组[2]和数组[3]的C和D。但是,如果继续滚动,A和B将再次出现,但这次它将显示数组[4]和数组[5]的信息。然后,如果您继续滚动,它将再次显示单元格C和D,但包含数组[6]和数组[7]的信息。这就是重复使用细胞的想法

因此,在您的情况下,您正在将动画应用到单元A,但当您滚动时,您将继续在“其他单元”上看到此动画。这些其他单元实际上是被重用的单元A

解决方案是,每当调用
cellForItemAtIndexPath:
时,即

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
    /* 
    Stop animation in case the cell is already being animated
    */
    if (indexPath.row == 0)
    {
        //do the animation