ios - 如何在 swift 3 中删除数组中的模型类数据?

标签 ios arrays swift

在这里,当从使用模型类保存的数组中按下删除按钮时,我试图删除特定的索引数据,任何人都可以帮助我从数组中删除数据并且不显示在 Collection View 中吗?

这里我试过这段代码

这里我声明了这样的数组

var cartItemsModel = [CartItems]()

下面是我的删除按钮操作

 @objc func deleteAction(sender : UIButton){
        let buttonPosition = sender.convert(CGPoint(), to: cartCollectionView)
        let indexPath = cartCollectionView.indexPathForItem(at: buttonPosition)
        self.deleteIndexPath = indexPath
        self.Index = sender.tag
        let obj = cartItemsModel[(indexPath?.row)!]
        self.cartItemsModel.remove(at: (indexPath?.row)!)
        self.itemId = obj.itemID
        self.price = obj.price
        //        view.isUserInteractionEnabled = false
        let attribute = RappleActivityIndicatorView.attribute(style: .apple, tintColor: .white, screenBG: .darkGray, progressBG: .black, progressBarBG: .orange, progreeBarFill: .red, thickness: 4)
        RappleActivityIndicatorView.startAnimatingWithLabel("Processing...", attributes: attribute)
        //        UIApplication.shared.beginIgnoringInteractionEvents()
        if loginCheck == 1 {
            let customerDeleteAPI = "\(domainName)/V1/carts/mine/items/\(itemId!)/"
            customerItemsDeleteDownloadJsonWithURL(customerItemsAPI: customerDeleteAPI)
        }else {
            let deleteApi = "\(domainName)/en/V1/guest-carts/\(self.guestQuoteId!)/items/\(itemId!)/"
            deleteItemsDownloadJsonWithURL(deleteItemsAPI: deleteApi)
        }
    }

下面是在根据响应调用删除 api 之后,我试图删除选择的数组,代码是

self.cartItemsModel.remove(at: (self.deleteIndexPath?.row)!)

我尝试了下面显示的另一个代码

self.cartItemsModel.remove(at: self.Index!)

最佳答案

请检查下面标记的答案。它会解决您的问题。

delete item from collection view

但在您成功调用 API 后使用这些方法。

此外,在 collectionView.deleteItems(at: [])

之前先从数组中删除项目

关于ios - 如何在 swift 3 中删除数组中的模型类数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49644695/

相关文章:

iphone - startDownloadingUbiquitousItemAtURL 本地文件路径?

ios - Swift UIButton 子类化并根据变量改变颜色

javascript - 根据键值的存在从数组中删除元素

Ubuntu 上的 Swift - 没有这样的文件或目录

ios - 长按手势识别器仅在手指抬起时触发

ios - NetStream http 视频无法在 IOS 设备上播放

javascript - 合并和转换对象中的键和数组的最佳方法

python - python对象的numpy数组

swift - 快速打印图像后的数字列表是什么

ios - 我如何在 obj-c 的 block 中通过引用发送参数