在被点击的单元格的索引处更改数据源

时间:2015-11-28 03:00:27

标签: ios swift uicollectionviewcell collectionview

我希望在点击时更改显示图像并在我的collectionView的单元格中隐藏标签。但随着细胞的重复使用,还有其他细胞设置为在滚动时显示图像。 我怎么能防止。我已经开始设置单元格的标签,但我不知道如何继续。

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    println("user tapped on cell number \(indexPath.row)")

    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

    cell.tag = indexPath.row

    if (cell.tag == 0) {
        one = true

            if (cell.myLabel.hidden) {
                cell.myLabel.hidden = false
                cell.MyImageView.image = nil

            }
            else {
                cell.myLabel.hidden = true
                cell.MyImageView.image = UIImage(named:"1")!
            }
    }

1 个答案:

答案 0 :(得分:1)

你应该尝试使用单元格所具有的selected属性, 当用户从突出显示的单元格中抬起时,切换选定的状态。然后您可以围绕它设置条件。

    if cell?.selected == true {// do whatever you want}  else{// same do whatever you want}

我认为您不需要设置标记,因为您可以在该方法中使用indexPath。