Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用自定义操作删除节中的单元格?_Ios_Swift_Uitableview_Delete Row - Fatal编程技术网

Ios 如何使用自定义操作删除节中的单元格?

Ios 如何使用自定义操作删除节中的单元格?,ios,swift,uitableview,delete-row,Ios,Swift,Uitableview,Delete Row,我也一直在努力使用trailingswipeactionscoconfigurationforrowat在tableview中删除我的单元格。我已将其设置为您滑动单元格的位置。当按下删除图像时,会显示删除图像,并显示alertView,询问您是否要删除该行。但一旦按下“是”,单元格不会被删除/删除 import UIKit class CartViewController: UIViewController { var selectedProduct: ItemList!

我也一直在努力使用
trailingswipeactionscoconfigurationforrowat
在tableview中删除我的单元格。我已将其设置为您滑动单元格的位置。当按下删除图像时,会显示删除图像,并显示alertView,询问您是否要删除该行。但一旦按下“是”,单元格不会被删除/删除

import UIKit

class CartViewController: UIViewController {

    var selectedProduct: ItemList!       // allows data to be passed into the CartVC

    // allows data to be sepearted into sections
    var cartItems: [CartItem] = []
    var groupedItems: [String: [CartItem]] = [:]
    var brandTitle: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        groupedItems = Dictionary(grouping: cartItems, by: {$0.itemList.brandName})
        brandTitle = groupedItems.map{$0.key}.sorted()
    }
}

extension CartViewController: UITableViewDelegate, UITableViewDataSource{

    func numberOfSections(in tableView: UITableView) -> Int {
        return brandTitle.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let brand = brandTitle[section]
        return groupedItems[brand]!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cartCell = tableView.dequeueReusableCell(withIdentifier: "CartCell") as! CartCell

        let brand = brandTitle[indexPath.section]
        let itemsToDisplay = groupedItems[brand]![indexPath.row]
        cartCell.configure(withCartItems: itemsToDisplay.productList)

        return cartCell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cartHeader = tableView.dequeueReusableCell(withIdentifier: "CartHeader") as! CartHeader

        let headerTitle = brandTitle[section]
        cartHeader.brandName.text = "Brand: \(headerTitle)"

        return cartHeader
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let cartFooter = tableView.dequeueReusableCell(withIdentifier: "CartFooter") as! CartFooter

        let brand = brandTitle[section]
        let subtotal = groupedItems[brand]?.map { $0.getCartTotal() }.reduce(0, +) ?? 0
        cartFooter.cartTotal.text = String(subtotal)

        return cartFooter
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 150
    }

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let edit = UIContextualAction(style: .normal, title: "") { (action, view, nil) in
            let refreshAlert = UIAlertController(title: "Deletion", message: "Are you sure you want to remove this item from cart? ", preferredStyle: .alert)

            refreshAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in

            }))

            refreshAlert.addAction(UIAlertAction(title: "No", style: .default, handler: { (action: UIAlertAction!) in
                refreshAlert .dismiss(animated: true, completion: nil)
            }))

            self.present(refreshAlert, animated: true, completion: nil)
        }
        edit.backgroundColor = .red
        edit.image = #imageLiteral(resourceName: "best_sellers")
        let config = UISwipeActionsConfiguration(actions: [edit])
        config.performsFirstActionWithFullSwipe = false
        return config
    }
}

iOS检查删除操作前后的行数,并期望它们在更改后正确相加。在警报操作中,您需要删除当前对象

refreshAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
             tableView.beginUpdates()
             // brandTitle[indexPath.section].remove(at: indexPath.row) // or you can directly use this

              let brand = brandTitle[indexPath.section]                                        
                 groupedItems[brand]!.remove(at:indexPath.row)
                   tableView.deleteRows(at: [indexPath], with: .fade)
                   tableView.endUpdates()
         }))

我在
groupedItems[brand]中遇到两个错误!。removeAtIndex(indexPath.row)
groupedItems.deleteRowsAtIndexPaths([indexPath],withRowAnimation:nil)
错误是类型为“[CartItem]”的值没有成员“removeAtIndex”,类型为“[String:[CartItem]”的值'没有成员'deleteRowsAtIndexPaths',并且我在写下该内容之前意外提交了我的道歉。它成功地删除了单元格,但也删除了页眉和页脚。如果节中只有一个单元格,这是可以的,但是如果节中有多个项目,并且删除了一个单元格,则会丢失节中所有单元格的页眉和页脚,如果这样做没有任何意义,但是,对于表视图的数据源,始终建议避免使用多个
数组
s,而使用
字典
为表视图提供数据不是一个好主意。你应该创建一个
模型
类型。@nayem我花了很长时间才弄明白如何创建那些数组来让我的购物车工作。但你是对的,我越看越觉得你是对的。但我更困惑的是如何将它转换为您的方式或其他可能的方式。我刚刚发布了一个问题,如果你不介意的话,我想请你帮忙解决这个问题。