简体   繁体   中英

How to reload data from custom view cell?

i have

class CartViewController: UIViewController,UITableViewDataSource,UITableViewDelegate

and

class CartTableViewCell: UITableViewCell

How to update CartViewController from CartTableViewCell file

class CartTableViewCell: UITableViewCell {
@IBAction func buttonDeleteAction(_ sender: UIButton) {
    let url = "http://aaaaaa/cart/"+label.text!+"/del"
    let headers: HTTPHeaders = ["Accept":"application/json","Authorization":"Bearer "+token!]
    Alamofire.request(url, method: .get, headers: headers).responseJSON { response in
        switch response.result {
        case .success(let value):
            let json = JSON(value)
            print("delete json ",json)
            let success = json["success"].boolValue
            print(success)
            if success == true{
                //UPDATE CARTVIEWCONTROLLER
            }
        case .failure(let error):
            print(error)

        }

    }
}

I looked for similar problems on the Internet but I did not find a suitable explanation

You need a protocol

protocol ReloadManager {
  func callReload(cell:CartTableViewCell) 
}

//

class CartViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,ReloadManager {

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

      let cell = tableView.dequeueReusableCell(withIdentifier:"id") as! CartTableViewCell

      cell.delegate = self

    }
    func callReload(cell:CartTableViewCell) {

       cartsTableView.reloadData()

       // use cell if you want to get indexpath to remove item from the table
    }

}

//

class CartTableViewCell: UITableViewCell {
   var delegate:ReloadManager?
   func btnClicked(_ sender:UIButton){
      delegate?.callReload(cell:self)
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM