swiftuser123
swiftuser123

Reputation: 43

swift tableview is not reloading after deleting cell

I have Tableview created in storyboard . when I try to delete the cell in tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction] Table is not reloading immediately after deleting. It will be successfully deleted in Database also. When I close the view and come back then it will be updated. Where am I going wrong?

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {


    let deleteAction = UITableViewRowAction(style: .normal, title: "Löschen") { (rowAction, indexPath) in


            let Info: NSDictionary = self.finaltaskValue[indexPath.item] as! NSDictionary
            let pwid = Info["pwid"] as! String
            self.finaltaskValue.remove(at: indexPath.row)
            self.finalTasktableView.beginUpdates()
            self.finalTasktableView.deleteRows(at: [indexPath], with: .none)
            self.finalTasktableView.endUpdates()


            let url = URL(string: "...php")

            var request = URLRequest(url: url!)
            request.httpMethod = "POST"
            let body = "parameters"

            request.httpBody = body.data(using: .utf8)

            URLSession.shared.dataTask(with: request) { data, response, error in

                if error == nil {

                    DispatchQueue.main.async(execute: {

                        do{
                            let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary


                            guard let parseJSON = json else {
                                print("")
                                return
                            }

                           DispatchQueue.main.async(execute: {
                            self.finalTasktableView.reloadData()
                            })

                        }catch {
                            //print("Caught an error: \(error)")
                            DispatchQueue.main.async(execute: {
                                let message = "Server ist Offline! Wir arbeiten mit hochdruck dran alles zu reparieren"

                        }
                    })

                }else {

                    DispatchQueue.main.async(execute: {
                        let message = error!.localizedDescription


                }

                }.resume()
        }


    deleteAction.backgroundColor = colorDarkGreen
    return [deleteAction]
}

Upvotes: 1

Views: 1133

Answers (2)

ingconti
ingconti

Reputation: 11666

1) I agree finaltasktableview.reloadData() is better for relatively small tables.

2) You issue a network call, but the first DispatchQueue.main.async(execute: {

is useless.

Upvotes: 0

Mitul.Patel
Mitul.Patel

Reputation: 252

1) You have to check your tableview get data from the final task value. 2) Please when you perform delete action don't use begin update and begin end update method please user reload method which shown here.

finaltasktableview.reloadData()

3) I don't understand why you define url request here? 4) your request not using any parameter of this method or delete logic parameter also not included. check your request response fill again that data which you want to delete.

Upvotes: 1

Related Questions