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)
}
}
}
}
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)
}
}