【问题标题】:Change the data source at the index of the cell that was tapped在被点击的单元格的索引处更改数据源
【发布时间】:2015-11-28 03:00:26
【问题描述】:

我想在点击时更改显示图像并在我的 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")!
            }
    }

【问题讨论】:

    标签: ios swift uicollectionviewcell collectionview


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      相关资源
      最近更新 更多