如果我在tableview中删除其中一项,如何删除所有项目?

问题描述 投票:0回答:1

我有两个

collectionView
一个用于主项目,另一个用于子项目

我们可以考虑的主要项目是iPhone

我们可以考虑作为苹果耳机的子项目

所以我们可以说这两个项目是“一组”

这两个项目都在 DidSelectMethod 处添加并显示在 tableView 中

我有一个删除此项目的选项

我的要求是,如果我从该组中删除任何一项,我的所有表格视图数据都应该被清除

这是我用来删除产品的代码

//在cellForRowAtIndexPath中定义按钮动作

cell.btnDelete.addTarget(self, action:#selector(ViewController.btnDeleteTapped(_:)), forControlEvents: .TouchUpInside)
cell.btnDelete.tag = indexPath.row

//按钮操作

func btnDeleteTapped(sender: UIButton){

  self.arrProductCart.removeObjectAtIndex(sender.tag)
  self.tblCart.reloadData()
}

主要产品阵列

(
    {
    "addtional_price" = 0;
    "barcode_image" = ".png";
    "cart_unique_id" = 6;
    "chicken_product" = all;
    "created_by" = 2;
    "created_date" = "2015-11-19 21:39:24";
    "deleted_date" = "0000-00-00 00:00:00";
    ids = 151;
    isFromMain = 1;
    "is_deleted" = 0;
    "is_has_addtional_price" = 0;
    "parent_product_unique_id" = 6;
    "product_id" = 53;
    "product_name" = "Black Pepper Chicken";
    "product_parent_id" = 0;
    "product_price" = "7.9";
    "sub_category_type" = "";
    "update_date" = "2016-06-22 17:54:53";
    "updated_by" = 7;
  "parent_product_unique_id" = 6
}

子产品数组

     {
    "created_by" = 2;
    "created_date" = "2015-11-19 23:11:37";
    "deleted_date" = "0000-00-00 00:00:00";
    ids = 157;
    isFromMain = 0;
    "product_category_id" = 16;
    "product_description" = "Potato";
    "product_id" = 59;
    "product_name" = "Potato";
    "product_parent_id" = 0;
    "product_price" = 2;
    "product_quantity" = 0;
    "sub_category_type" = s;
    "product_unique_id" = 6
}

有什么解决方案可以做到这一点吗?

ios swift uitableview
1个回答
0
投票

要实现删除主项或子项会清除整个 tableView 数据的所需功能,您需要修改删除操作,以便检查删除的项是否是组的一部分,如果是,则清除所有数据。

// Define Button Action in cellForRowAtIndexPath
cell.btnDelete.addTarget(self, action: #selector(ViewController.btnDeleteTapped(_:)), for: .touchUpInside)
cell.btnDelete.tag = indexPath.row

// Button Action
@objc func btnDeleteTapped(sender: UIButton) {
    let productToDelete = arrProductCart[sender.tag] as! [String: Any]
    let parentProductUniqueId = productToDelete["parent_product_unique_id"] as? Int
    
    // Check if the item belongs to a group
    if let parentProductUniqueId = parentProductUniqueId {
        // If it belongs to a group, clear all data
        self.arrProductCart.removeAll()
    } else {
        // Otherwise, remove the individual item
        self.arrProductCart.remove(at: sender.tag)
    }
    
    self.tblCart.reloadData()
}
© www.soinside.com 2019 - 2024. All rights reserved.