Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove a cell from a TableView from another ViewController when tap on UIButton (Swift)

There are 2 ViewController’s. On the first ViewController one there is a table with cells. When you click on a cell, a second ViewController opens, on which information from the cell is located and a button, when press, the cell should be deleted and a return to the first ViewController should occur.

I get indexForCell in the cellForRowAt method

  var data: [String] = []
  var indexForCell: Int?

    private func configData() {
        service.addMessage(offset: offsetStart) { [weak self] result in
            switch result {
            case .success(let dataMessage):
                self?.data = dataMessage
                DispatchQueue.main.async {
                    self?.messageTable.reloadData()
                }
            case.failure(let error):
                print(error)
                DispatchQueue.main.async {
                    self?.showAlert(title: "Error", message: "Error connecting to the server")
                }
            }
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let textMessage = data[indexPath.row]
        let infoController = InfoController()
        
        
        infoController.messageLabel.text = textMessage
        indexForCell = indexPath.row
        navigationController?.pushViewController(infoController, animated: true)
    }

But when I access my indexForCell from the second ViewController, I understand that my data is empty and the indexForCell = nil

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let startController = StartController()
private let deleteButton = CustomButton()

    @objc private func tapDeleteButton() {
        
       //delete cell from 1st ViewController
        startController.messageTable.reloadData()
        navigationController?.popViewController(animated: true)
    }

What do I need to add to func tapDeleteButton() to successfully delete a cell?

Thank you so much in advance!

>Solution :

Inside InfoController add this property

 weak var delegate:StartController?

Then set it here

 let infoController = InfoController()
 infoController.delegate = self

Finally you can use it to delete

delegate?.deleteItem()
navigationController?.popViewController(animated: true)

And create deleteItem method inside StartController to do the process using indexForCell that you set before

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading