ios - 如何使用原始索引路径从数组中删除对象?

标签 ios swift

我有一个 UICollectionView,用户可以在其中从单元格中选择数据以添加到数组中。我试图在点击时突出显示选定的单元格,并在再次点击时取消突出显示它们。在突出显示和取消突出显示的同一段代码中,我想从数组中添加/删除数据。将数据添加到数组中没有问题,但我不知道如何在未突出显示时将其删除。

代码在这里:

var removeFromList = [AnyObject]()
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {

    var cell = self.collectionView2.cellForItemAtIndexPath(indexPath)

    if cell!.tag == 0 {
        cell!.layer.borderWidth = 2.0
        cell!.layer.borderColor = UIColor.blueColor().CGColor
        removeFromList.append(objectIds[indexPath.row])
        cell!.tag = 1
    } else {
        cell!.layer.borderWidth = 0.0
        cell!.tag = 0
        removeFromList.//WHAT CAN I PUT HERE?
    }
    return true
}

最佳答案

使用removeAtIndex(index: Int)方法移除一个项目

var removeFromList = [NSString]()

 if let index = find(removeFromList, objectIds[indexPath.row] as! NSString) {
     removeFromList.removeAtIndex(index)
 }

关于ios - 如何使用原始索引路径从数组中删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30681894/

相关文章:

ios - 为什么我无法使用 swift 3 在 iOS 上获取本地通知?

ios - 更新现有实体时核心数据模型配置不兼容

ios - 展开可选值时,Customer Cell 返回 nil

ios - 委托(delegate)调用后 Swift 对象不会更新

ios - 使用 Abid 打开 WhatsApp 对话不起作用

android - 为什么 TYPE_LINEAR_ACCELERATION 在设备静止时产生非零值?

ios - (Swift) 选中的 tabBar 颜色?

ios - 在类 AppDelegate 上发出 SIGABRT 信号

swift - SpriteKit SKTextures

ios - 如何在 Swift 5 中选择文件的屏幕上创建取消按钮?