Search code examples
iosswiftuitableviewsegueswipe

UITableView swipe to edit


I have two uitableviewcontroller , one is lists table and the other is editing table. I use accessory action to edit item. it works fine.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if  segue.identifier == "EditItem" {
        let controller = segue.destination as! ItemDetailViewController
        controller.delegate = self
        if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) {

            controller.itemToEdit = items[indexPath.row]
        }
    }
}

now I add swipe function to edit item

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let edit = UIContextualAction(style: .normal, title: "Edit") { action, view, completion in
        self.performSegue(withIdentifier: "EditItem", sender: self)

        completion(true)
    }

    let delete = UIContextualAction(style: .destructive, title: "Delete") { [weak self] action, view, completion in
        self?.items.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        self?.saveChannelListItems()
        completion(true)
    }

    edit.backgroundColor = .purple
    edit.image = #imageLiteral(resourceName: "edit")
    delete.backgroundColor = .red
    delete.image = #imageLiteral(resourceName: "delete")


    return UISwipeActionsConfiguration(actions: [delete, edit])
}

then I have errors.

Could not cast value of type 'abc.ListController' (0x1089b1398) to 'UITableViewCell' (0x1150f18e0).

how can I swipe to edit items?


Solution

  • Here you send self which is the vc in sender parameter

    self.performSegue(withIdentifier: "EditItem", sender: self)
    

    and force unwrap it to the cell which is the problem

    if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) {
    

    Instead you can do

    self.performSegue(withIdentifier: "EditItem", sender:items[indexPath.row])
    

    with

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if  segue.identifier == "EditItem" {
             let controller = segue.destination as! ItemDetailViewController
             controller.delegate = self 
             controller.itemToEdit = sender as! ItemType // where ItemType is the type of the array elements 
            }
        }
    }