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

我也很努力地使用trailingSwipeactionsConfigurationForRowAt删除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",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
    }
}
sunvisual 回答:如何使用自定义操作删除部分中的单元格?

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()
         }))
本文链接:https://www.f2er.com/2832286.html

大家都在问