Deleting item in struct from tableView

I want to be able to delete items in my struct with a button in a tableView. I have implemented the button in the tableViewCell with delegate, and this seems to work fine (print("test") works when I press the button.

But how can I delete a certain item in the struct from the tableView?

Here are some code, and what I have tried so far:


Struct:

struct CartItem {
    var name: String = ""
    var count = Int()
}

var itemsInCart : [CartItem] = []


TableViewCellFile:

protocol RemoveItemDelegate: NSObjectProtocol {
    func didDeleteItemCell(cell: CheckOutTableViewCell)
  
}

class CheckOutTableViewCell: UITableViewCell {
   
    @IBOutlet weak var checkOutLabel: UILabel!
    weak var delegate: RemoveItemDelegate?
   
    @IBAction func deleteButton(_ sender: UIButton) {
        delegate?.didDeleteItemCell(cell: self)
    }
}


TableViewController:

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemsInCart.count
    }
   
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CheckOutCell") as! CheckOutTableViewCell
     
       
        let name = itemsInCart[indexPath.row].name
        let number = itemsInCart[indexPath.row].count
       
        cell.checkOutLabel.text = "\(number) \(name)"
       
        cell.delegate = self
       
        return cell
    }
   
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }


}

extension CheckOutViewController: RemoveItemDelegate
{
    func didDeleteItemCell(cell: CheckOutTableViewCell) {
    
        print("test")
       
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "CheckOutCell") as! CheckOutTableViewCell
            if let index = itemsInCart.index(where: {$0.name == cell.checkOutLabel.text})   {
                itemsInCart.remove(at: index)
                let indexPath = IndexPath(row: index, section: 0)
                tableView.deleteRows(at: [indexPath], with: .fade)
       
            }
        }
    }
}
Answered by Claude31 in 326674022

You put

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { }


inside another func, so not visible.

In addition, you must not use cell as a storage: it is not, it is just for display

How do you delete the row: by a user action ? Or when tapping a button outside of cell?


In addition, why do you need it ?

Just remove the item from the dataStore and call from the table view


The best way to do this is to use



    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    
        if editingStyle == .delete {
             itemsInCart.remove(at: indexPath.row)
             DispatchQueue.main.async { // Needed to redraw
                    tableView.deleteRows(at: [indexPath], with: .fade)
             }
          }
    }


If you want to delete a given cell directly from a button (not the delete in the cell)


func deleteCell(cellName: String) {

            if let index = itemsInCart.index(where: {$0.name == cellName})   {
                itemsInCart.remove(at: index)
                let indexPath = IndexPath(row: index, section: 0)
                tableView.deleteRows(at: [indexPath], with: .fade)
            }
}
Accepted Answer

You put

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { }


inside another func, so not visible.

In addition, you must not use cell as a storage: it is not, it is just for display

How do you delete the row: by a user action ? Or when tapping a button outside of cell?


In addition, why do you need it ?

Just remove the item from the dataStore and call from the table view


The best way to do this is to use



    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    
        if editingStyle == .delete {
             itemsInCart.remove(at: indexPath.row)
             DispatchQueue.main.async { // Needed to redraw
                    tableView.deleteRows(at: [indexPath], with: .fade)
             }
          }
    }


If you want to delete a given cell directly from a button (not the delete in the cell)


func deleteCell(cellName: String) {

            if let index = itemsInCart.index(where: {$0.name == cellName})   {
                itemsInCart.remove(at: index)
                let indexPath = IndexPath(row: index, section: 0)
                tableView.deleteRows(at: [indexPath], with: .fade)
            }
}

Sorry for being unclear. I want the user to be able to delete items that is being displayed in the tableView. The way they should be able to delete, is to tap a deletebutton on the right side of the cell.

I have hooked up the deleteButton in the TableViewCellFile, and where trying to use it with the "extension CheckOutViewController: RemoveItemDelegate" in the bottom of my question. How and where should I implement the button to delete an item from the struct?


P.S. This worked perfectly, and is exatctly what I want to accomplish, only with a button

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
        if editingStyle == .delete { 
             itemsInCart.remove(at: indexPath.row) 
             DispatchQueue.main.async { // Needed to redraw 
                    tableView.deleteRows(at: [indexPath], with: .fade) 
             } 
          } 
    }

I want the user to be able to delete items that is being displayed in the tableView. The way they should be able to delete, is to tap a deletebutton on the right side of the cell.


In that case, the first solution

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { }


should do the work ?


How and where should I implement the button

When you swipe the cell to the left, the delete button appears.


What else ?

Deleting item in struct from tableView
 
 
Q