0

I have a view controller containing a table view. In the table view are cells that have a button to delete the data the cell contains. When I press the delete button the cell information is removed from my database but the table view doesn't update until i segue back and forth.

I'm unsure how to access the TableView and force it to re-draw itself from a method inside my table cell.

class EditTaskTableViewCell: UITableViewCell {

   //......//

    @IBAction func deleteButtonPressed(_ sender: Any) {
        let userDefaults = Foundation.UserDefaults.standard
        if let userTasks = userDefaults.array(forKey: "userTasks"){
            let newArray = removeTask(item: taskNameLabel.text!, from: userTasks as! [String])
            userDefaults.set(newArray, forKey: "userTasks")
            userDefaults.synchronize()
        }

        //update table view 

    }
}


class EditTaskTable_VC: UIViewController, UITableViewDelegate, UITableViewDataSource {

   //......//

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

           let cell = Bundle
           .main
           .loadNibNamed("EditTaskTableViewCell", owner: self, options: nil)?
           .first as! EditTaskTableViewCell

          cell.taskName.text = cellData[indexPath.row].title 

          return cell       

   }

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return cellData.count
   }
Wikkle_A
  • 145
  • 2
  • 12

1 Answers1

-1

Try to use delegates.

The example is in Objective C but the general idea is there.

sending reload data from custom tableview cell?

swift delegate example

https://gist.github.com/austinzheng/db58036c4eb825e63e88

Community
  • 1
  • 1
user12345625
  • 398
  • 6
  • 17