在cellForRowAtIndexPath中重新排序collectionViewCells的目标

时间:2016-05-12 08:45:55

标签: ios swift uicollectionviewcell

我正在尝试在我的collectionViewCell中实现一个行为。

Cell检查是否有该单元格的图像。 如果有图像,您将获得一个十字符号(用于删除) 如果没有图像,您将获得一个加号(用于添加)

每当你点击一个plussymbol时触发一个方法,只要点击一个十字符号,就会触发另一个方法。

见我的

cellForRowAtIndexPath {
    if let imageId = imageIds[indexPath.row] as? String {

    if let imageData = imageForId[imageId]{

                collectionCell.collectionViewImage.image = UIImage(data:imageData)
                collectionCell.collectionViewButton.setImage(UIImage(named: "kreuzzeichen.png"), forState: UIControlState.Normal)
                 collectionCell.collectionViewButton!.addTarget(self, action: "showDeleteAlert:", forControlEvents: UIControlEvents.TouchUpInside)
            }

        }
    } else{

        collectionCell.collectionViewButton.setImage(UIImage(named: "pluszeichen.png"), forState: .Normal)
        collectionCell.collectionViewImage.image = nil
        collectionCell.collectionViewButton.removeTarget(self, action: "showDeleteAlert:", forControlEvents: UIControlEvents.TouchUpInside)
        collectionCell.collectionViewButton!.addTarget(self, action: "pickImage", forControlEvents: UIControlEvents.TouchUpInside)

    }

}

然而,删除其中一个图像会使collectionView对其进行重新排序。如有必要,交换符号,但目标不是。

有没有办法刷新这个?

我试过检查是否:

collectionCell.collectionViewImage.setImage == UIImage(named:"kreuzzeichen.png")

但是它不会比较那些,所以我的代码后来没有用。

1 个答案:

答案 0 :(得分:1)

  

在if条件中删除pickImage目标

if let imageData = imageForId[imageId]{

         collectionCell.collectionViewImage.image = UIImage(data:imageData)
         collectionCell.collectionViewButton.setImage(UIImage(named: "kreuzzeichen.png"), forState: UIControlState.Normal)
         collectionCell.collectionViewButton!.addTarget(self, action: "showDeleteAlert:", forControlEvents: UIControlEvents.TouchUpInside)
         collectionCell.collectionViewButton.removeTarget(self, action: "pickImage:", forControlEvents: UIControlEvents.TouchUpInside)
    }
     else {
        collectionCell.collectionViewButton.setImage(UIImage(named: "pluszeichen.png"), forState: .Normal)
        collectionCell.collectionViewImage.image = nil
        collectionCell.collectionViewButton.removeTarget(self, action: "showDeleteAlert:", forControlEvents: UIControlEvents.TouchUpInside)
        collectionCell.collectionViewButton!.addTarget(self, action: "pickImage", forControlEvents: UIControlEvents.TouchUpInside)

    }