【发布时间】:2021-10-09 04:46:35
【问题描述】:
我成功获取 JSON 数据,当我单击 cancelTapped 按钮时.. 然后.. 服务从 JSON 中删除,但在 tableview 行中仍然显示,为什么
取消服务后,我不需要显示该行
代码:这里cancelid 是我需要从tableview 中删除该服务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()
}
}
【问题讨论】:
-
问题是您可能正在删除请求,因为您没有更新数据源,即
wishlistData?.result?.wishlist?。在取消 API 成功后重新加载单元之前,您还应该更新wishlistData?.result?.wishlist?variable。 -
@Kudos 什么是数据源,是服务调用..如何更新..请指导
-
@如果我在重新加载之前喜欢这个
self?.wishlistServicecall().. 然后又添加了一项服务 -
肯定会的。所以不要使用它,只需在下面查看我的答案。
标签: json swift button tableview