如何在调用删除按钮Api后删除模型类中保存的数据?

时间:2017-11-08 10:11:43

标签: ios swift

这里在我的愿望清单页面中我点击删除按钮后点击我正在调用api来删除服务器中的项目但是删除它后服务器成功但在表格视图中我无法删除它可以任何人帮助我如何删除这个和这里我从模型类传递的数据?

这是我的代码

func deleteButtonAction(button: UIButton){
        let buttonPosition = button.convert(CGPoint(), to: tableDetails)
        let index = tableDetails.indexPathForRow(at: buttonPosition)
        let obj = wishListModel[(index?.row)!]
        let deleteURL = "http://magento.selldesk.io/index.php/rest/V1/wishlist/delete/\(obj.wishlistItemId!)?customer_id=\(self.customerId!)"
        self.deleteWishListItemsDownloadJsonWithURl(deleteApi: deleteURL)
    }
func deleteWishListItemsDownloadJsonWithURl(deleteApi: String){
        let url = URL(string: deleteApi)
        var request = URLRequest(url: url! as URL)
        request.httpMethod = "DELETE"

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if error != nil { print(error!); return }
            do {
                if let jsonObj = String(data: data!, encoding: .utf8) {
                    print(jsonObj)
                    DispatchQueue.main.async {
                       self.tableDetails.reloadData()
                    }
                }
            } catch {
                print(error)
            }
        }
        task.resume()
    }

3 个答案:

答案 0 :(得分:0)

如果您要删除特定索引,请致电wishListModel.removeAtIndex(index)  否则,如果要从wishListModel中删除所有对象,请在服务器的成功响应中使用wishListModel.removeAll()。

P.S确保在调用self.tableDetails.reloadData()

之前删除对象

答案 1 :(得分:0)

如果您的api删除了特定项目,那么您需要从模型中删除索引,并且永远不会忘记在删除后重新加载数据。

wishListModel.removeAtIndex(index_number)
self.tableDetails.reloadData()

但如果您的api从服务器端删除了所有项目,则需要执行此操作。

wishListModel.removeAll()

之后永远不要忘记在tableview中重新加载数据:)

self.tableDetails.reloadData()

答案 2 :(得分:0)

我在代码中做了一些更改,你可以尝试一下。

func deleteButtonAction(button: UIButton){
        let buttonPosition = button.convert(CGPoint(), to: tableDetails)
        let index = tableDetails.indexPathForRow(at: buttonPosition)
        let obj = wishListModel[(index?.row)!]
        let deleteURL = "http://magento.selldesk.io/index.php/rest/V1/wishlist/delete/\(obj.wishlistItemId!)?customer_id=\(self.customerId!)"
        self.deleteWishListItemsDownloadJsonWithURl(deleteApi: deleteURL, atIndex:index)
    }
func deleteWishListItemsDownloadJsonWithURl(deleteApi: String, atIndex:IndexPath){
        let url = URL(string: deleteApi)
        var request = URLRequest(url: url! as URL)
        request.httpMethod = "DELETE"

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if error != nil { print(error!); return }
            do {
                if let jsonObj = String(data: data!, encoding: .utf8) {
                    print(jsonObj)
                    DispatchQueue.main.async {
                       wishListModel.removeAtIndex(atIndex.row)
                       self.tableDetails.reloadData()
                    }
                }
            } catch {
                print(error)
            }
        }
        task.resume()
    }