繁体   English   中英

如何从自定义视图单元格重新加载数据?

[英]How to reload data from custom view cell?

我有

class CartViewController: UIViewController,UITableViewDataSource,UITableViewDelegate

class CartTableViewCell: UITableViewCell

如何从CartTableViewCell文件更新CartViewController

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)

        }

    }
}

我在互联网上寻找类似的问题,但没有找到合适的解释

您需要一个协议

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)
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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