how to use an UIContextualAction in a cell to send CoreData strings to an textfield placeholder

33 Views Asked by At

I have a list in a tableview. The cells are filled with data from CoreData. To add new data I have another ViewController.

ViewControllerAdd

To send the CoreDataContainer to the next ViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let nextVC = segue.destination as? ViewControllerAdd {
        nextVC.container = container
    }
}

To add the buttons for the cell:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let deleteAction = UIContextualAction(style: .destructive, title: nil) { (action, view, completionHandler) in
        let row = indexPath.row
        self.context.delete(ViewController.liste[row])
        ViewController.liste.remove(at: row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
        self.syncOrder()
        self.printData()
        completionHandler(true)
    }
    
    deleteAction.backgroundColor = .systemRed
    deleteAction.image = UIImage(systemName: "trash.fill")
    
    let editAction = UIContextualAction(style: .destructive, title: nil) { (action, view, completionHandler) in
        //TODO: Bearbeitung der Daten. Am besten zum ViewControllerAdd. In die leeren Textfields die Daten eintragen bzw. transparent
        //TODO: hinterlegen.
        self.performSegue(withIdentifier: "ViewControllerAdd", sender: self)
        print("Edit-Button")
        completionHandler(true)
    }
    
    editAction.backgroundColor = .systemYellow
    editAction.image = UIImage(systemName: "slider.horizontal.3")
    
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
    
    return configuration
}

I try to use to fill the textfield.placeholder with the data of CoreData. It should load the present data from the container. So I can edit the present data to CoreData I save it again.

0

There are 0 best solutions below