0

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.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Guenni
  • 1
  • 1
  • And your question is what? The example code seems to bare little relation to the title? – flanker May 14 '23 at 23:41
  • Are you wanting to use the same view controller for both add and edit operations? If so, I would suggest that you add a property of the type of your Core Data entity - Something like `var item: MyCoreDataType?` - Then in your edit button action, pass the item from the row as `sender:` in your `performSegue`. Now, in `prepareForSegue` if `sender` is an instance of `MyCoreDataType` you can assign it to `item` in your destination VC. In the add/edit VC you can load the properties based on `item` not being `nil`. If `item` is nil, create a new entity and assign it to `item` – Paulw11 May 15 '23 at 00:44
  • Yeah that’s right. I want to use the controller for both. If you use it via add button the text field is empty and if it’s clicked via edit button the items should be the Textfeld placeholder. – Guenni May 15 '23 at 10:15

0 Answers0