单击弹出菜单上的“是”按钮,表格视图未清除

时间:2019-10-17 06:10:48

标签: ios json swift xcode tableview

基本上我想在弹出按钮上删除从数据库中获取的JSON记录。数据已从JSON中删除,但仍显示在表格视图单元格中。要做的是刷新表。 任何帮助将不胜感激 谢谢

enter image description here

 let cancelAction = UIAlertAction(title: "OK", style: .default) { (_) in
        self.cancelbooking()
        //            mytableView.deleteRows(at: [indexPath], with: .middle)
        self.mytableView.reloadData()
    } 

这是json数据获取表记录的功能

 func cancelbooking(){

    let ph = defaults.string(forKey: "mobno")

    print("cancelbooking booking value1 \(bookingidvalue2)")
    print("cancelbooking booking date \(bookingdatevalue1)")


    var request = URLRequest(url: URL(string: "http://ottokonnect.com/api/charging-station/booking-cancel")!)
    request.httpMethod = "POST"
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    do{
        let postString = "booking_id="+bookingidvalue1+"&booking_date="+bookingdatevalue1+"&start_at="+startatvalue1+"&end_at="+endatvalue1+"&mobile="+ph!
        print("postrsing is \(postString)")
        request.httpBody = postString.data(using: .utf8)
        self.activityIndicator("Please Wait")
    }catch let error{
        //print(error.localizedDescription)
        return
    }

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

        if (error != nil){
            return
        }
        do{
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            if let parseJSON = json{
                //print(parseJSON)
                if let userId = parseJSON["error"] as! Int? {
                    let msg = parseJSON["message"] as! String?
                    if (userId == 0){
                        //print(userId)
                        DispatchQueue.main.async {
                            //                                self.showGlobalAlertwithMessage(msg as! NSString)
                            DispatchQueue.main.async {
                                self.effectView.removeFromSuperview()

                            }
                        }
                    }else {
                        DispatchQueue.main.async {
                            //                                self.showGlobalAlertwithMessage(msg as! NSString)
                            DispatchQueue.main.async {
                                self.effectView.removeFromSuperview()
                            }
                        }
                    }
                }

            } else{
                self.showGlobalAlertwithMessage("Could not successfully perform this request. Please try again later")
                DispatchQueue.main.async {
                    self.effectView.removeFromSuperview()
                }
            }


        }catch{
            self.showGlobalAlertwithMessage("Could not successfully perform this request. Please try again later")
            DispatchQueue.main.async {
                self.effectView.removeFromSuperview()
            }
            //print(error)
        }

    }
    task.resume()

}

2 个答案:

答案 0 :(得分:0)

self.cancelbooking()调用一个API,然后self.mytableView.reloadData()重新加载数据。在完成API调用之前,将调用self.mytableView.reloadData()。在do block的{​​{1}}中设置self.cancelbooking()。如果您的self.mytableView.reloadData()发送空数据,则tableView将为空。

答案 1 :(得分:-1)

首先,您需要从用作model的{​​{1}}中删除数据,然后调用TableViewDataSource