【问题标题】:Updating an array of integer Swift更新整数 Swift 数组
【发布时间】:2016-01-30 20:53:44
【问题描述】:

我有一组模拟数据,如下所示:

var demoInit = [("name", "amount", "place"), ("name", "amount", "place"), ("name", "amount", "place")]

我在集合视图中使用它,我希望我的用户不要选择超过三个,我做到了:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("interestAsset", forIndexPath: indexPath) as! InterestCell
    if selectedItems.count < 3 {
        selectedItems += [indexPath.row]
        selectedCell.holderView.alpha = 0.5
        selectedCell.holderView.backgroundColor = UIColor.greenColor()
        selectedCell.checkMark.hidden = false
    }
}

如果您注意到这一行 selectedItems += [indexPath.row] 是我存储 UICollectionView 中选定的 indexPath 的位置。但是我遇到的问题是,我想实现取消选择,但要实现它,我需要从selectedItems 数组中删除特定存储的indexPath。我怎么做?谢谢

【问题讨论】:

  • 看看我的回答,这应该可行...

标签: arrays swift uicollectionview


【解决方案1】:

您可以使用Array.removeAtIndexmethod 从数组中删除。

一个例子:

var intArray = [1,5,6,8,10]
//Remove 10
intArray.removeAtIndex(intArray.indexOf(10))
print("\(intArray)") // [1,5,6,8]

【讨论】:

    【解决方案2】:

    如果你想达到目的就使用它:

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("interestAsset", forIndexPath: indexPath) as! InterestCell
        if selectedItems.count < 3 {
            selectedItems += [indexPath.row]
            selectedCell.holderView.alpha = 0.5
            selectedCell.holderView.backgroundColor = UIColor.greenColor()
            selectedCell.checkMark.hidden = false
        }
        for (var index = 0; index < 3; ++index) {
            if(selectedItems[index] == indexPath.row)
            {
                 selectedItems.removeAtIndex(index)
            }
        }
    }
    

    解释:

    您正在使用 for 循环来遍历您选择的 3 个项目。如果其中之一等于您的 indexPath,那么它会跳入您的 if 函数。 在 if 函数内部,它只是简单地从 selectedItems 中删除 selectedItem。

    如果成功了就写信给我...

    【讨论】:

      【解决方案3】:

      无需手动跟踪UICollectionView 中的选定单元格。

      只需调用collectionView.indexPathsForSelectedItems() 即可检索与所选单元格相关的索引列表。

      你的代码变成这样

      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
          let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("interestAsset", forIndexPath: indexPath) as! InterestCell
          let selectedItems = collectionView.indexPathsForSelectedItems()?.count ?? 0
      
          if  selectedItems < 3 {
              selectedCell.holderView.alpha = 0.5
              selectedCell.holderView.backgroundColor = UIColor.greenColor()
              selectedCell.checkMark.hidden = false
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-03
        • 2019-11-25
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        相关资源
        最近更新 更多