无法使用按钮和 Swift 中的 JSON 数据删除表视图行

Unable to delete tableview row with button and JSON data in Swift

我正在成功获取 JSON 数据,当我单击 cancelTapped 按钮时......然后......服务已从 JSON 中删除,但在表视图中行仍在显示,为什么

取消服务后我不需要显示行

代码:这里 cancelid 是我需要从表视图中删除的服务,那个服务 ID

var cancelid: String?

override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}
func deleteServiceCall(){

print("cancel id : \(cancelid)")

let param = ["wishlist_id" : cancelid]
APIReqeustManager.serviceCall(param: param, method: .post, vc: self, url: CommonUrl.delete_request, isTokenNeeded: true, isErrorAlertNeeded: true, isSuccessAlertNeeded: false) { [weak self] (resp) in
    if let code = ((resp.dict?["result"] as? [String : Any])){
        let success = code["status"] as? [String : Any]
        if success == "Success"{
            DispatchQueue.main.async {
                self?.tableView.reloadData()
            }
        }
    }
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return wishlistData?.result?.wishlist?.count ?? 0
}

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

let cell = tableView.dequeueReusableCell(withIdentifier: "fundTableViewCell", for: indexPath) as! FundTableViewCell

let indexData = wishlistData?.result?.wishlist?[indexPath.row]

cancelid = String(indexData?.id ?? 0)
cell.cancelBtn.tag = indexPath.row
cell.cancelBtn.addTarget(self, action: #selector(cancelTapped(sender:)), for: UIControl.Event.touchUpInside)

return cell

}


@objc func cancelTapped(sender: UIButton) {

self.showTwoButtonAlertWithLeftAction(title: "WithDraw", buttonTitleLeft: "Yes", buttonTitleRight: "Cancel", message: "Do you want to remove this withdrawel Request!?") {
    self.deleteServiceCall()
}
}

[String : Any]String 相比总是失败。

status 的值很可能是字符串。

而且您还必须从数据源数组中删除该项。

if let code = resp.dict?["result"] as? [String : Any], 
    let success = code["status"] as? String, success == "Success" {
        DispatchQueue.main.async {
            if let index = self?.wishlistData?.result?.wishlist?.firstIndex(of: {[=10=].id == cancelid}) {
               self?.wishlistData?.result?.wishlist?.remove(at: index)
               self?.tableView.removeRows(at: [IndexPath(row: index, section: 0)], with: .fade)
            }
        }
}

注意:您应该将数据源数组声明为非可选的空数组,以摆脱所有这些可选项。

您需要在 self?.tableView.reloadData() 之前更新数据源。

  APIReqeustManager.serviceCall(param: param, method: .post, vc: self, url: CommonUrl.delete_request, isTokenNeeded: true, isErrorAlertNeeded: true, isSuccessAlertNeeded: false) { [weak self] (resp) in
    if let code = ((resp.dict?["result"] as? [String : Any])){
        let success = code["status"] as? [String : Any]
        if success == "Success"{
         //Before reloading tableView we should update datashouce i.e, variable you are storing data like:
            wishlistData?.result?.wishlist = resp.dic[""]
            DispatchQueue.main.async {
                self?.tableView.reloadData()
            }
        }
    }
}