【问题标题】:*Very Simple* Result of call to "== " is unused*非常简单*调用“==”的结果未使用
【发布时间】:2016-06-16 22:24:17
【问题描述】:

使用多个单元格实现 UICollectionView。

当您单击单元格时,我想将 selectedArray 中的值更改为 true。

这是我的代码:

var selectedArray = [Bool](count:201, repeatedValue:false)

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath)
    if cell?.selected == true {
      selectedArray[indexPath.row] == true
      print("\(selectedArray[indexPath.row])")
  }
}

我单击的每个单元格的输出都是错误的,因此我知道某些内容未正确链接,但我只是看不到它。

【问题讨论】:

  • NSMutableIndexSet 是一种更适合跟踪选择的数据结构。

标签: ios arrays swift uicollectionview uicollectionviewcell


【解决方案1】:

您无需检查单元格是否被选中。因为您正在调用didSelectItemAtIndexPath,所以您不需要检查这个。简单地运行这个:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    selectedArray[indexPath.row] = true
}

【讨论】:

    【解决方案2】:

    selectedArray[indexPath.row] == true - 需要将 '==' 替换为 '='

    【讨论】:

    • hhmm 我取出了 if cell?.selected 行,我仍然得到相同的结果@Savchenko
    • @Cameron,奇怪.. 你还用 selectedArray 做什么?
    • 什么都没有,我的函数中只有两行:selectedArray[indexPath.row] == true 和 print("(selectedArray[indexPath.row])")
    • @Cameron, selectedArray[indexPath.row] == true,可能需要将'=='替换为'='?
    • 双等号比较,不是设置
    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多