【问题标题】:Unable to delete tableview row with button and JSON data in Swift无法在 Swift 中删除带有按钮和 JSON 数据的 tableview 行
【发布时间】: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


【解决方案1】:

[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: {$0.id == cancelid}) {
               self?.wishlistData?.result?.wishlist?.remove(at: index)
               self?.tableView.removeRows(at: [IndexPath(row: index, section: 0)], with: .fade)
            }
        }
}

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

【讨论】:

  • 好的,我已经完成了你的回答.. 但是当我点击cancelTapped self?.tableView.reloadData() 时没有点击.. 从表中删除行.. 但在 JSON 响应服务中被删除..什么问题?
  • 您还必须从数据源数组中删除该项目
  • 怎么样? tableview 正在点击,但单元格没有删除
  • 您必须从wishList 中删除该项目。通过cancelId获取item的索引,调用remove(at:)
  • 错误:Value of type 'Array<wishlist>??' has no member 'firstIndex'
【解决方案2】:

您需要在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()
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多