通过单击单元格中的取消按钮删除该单元格,并通过editActionsForRowAt方法删除该单元格

时间:2019-04-08 08:23:33

标签: ios swift uitableview button

使用UITableView编辑方法可以正常工作,但是使用UIButton则无法正常工作。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (deleteAction, indexPath) in
        deleteApm(Id: myString)
        self.AArrayList.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
    return [deleteAction]
}

func deleteApm(Id:String)
{
    //...................................
}

@IBAction func myButt(_ sender: UIButton) {
    let cancelBtn = sender.tag
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ACell", for: indexPath) as! ATableViewCell
    cell.cancelBtn.addTarget(self, action: #selector(self.myButt(_:)), for: .touchUpInside)
    return cell
}

使用此方法,我在按钮中调用了相同的删除方法,但是使用UIButton删除操作无效。 使用UITableView可以正常工作...

2 个答案:

答案 0 :(得分:2)

在cellforRow中为按钮添加标签,以标识行或特定对象

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ACell", for: indexPath) as! ATableViewCell
     cell.cancelBtn.tag = indexPath.row
     cell.cancelBtn.addTarget(self, action: #selector(self.myButt(_:)), for: .touchUpInside)
 }

并以

处理该操作
@IBAction func myButt(_ sender: UIButton) {
    let indexPath: IndexPath = IndexPath(row: sender.tag, section: 0)
    deleteApm( currentIndexpath: indexPath)

}

以及通话中的deleteRow

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (deleteAction, indexPath) in
        self.deleteApm(currentIndexpath: indexPath)           
    }
    return [deleteAction]
}

最终创建删除api调用的常用方法

func deleteApm(currentIndexpath: IndexPath )    {
    let jsonDict = self.ArrayList[currentIndexpath.row] as? [String:Any]
    let Id:Int = jsonDict?["appointId"] as! Int
    let Id2 : Int = jsonDict?["appointId"] as! Int
    let param : [String : String] = ["pid": {String(Id2)}()]
    let headers = ["Content-Type": "application/x-www-form-urlencoded"]
    // "http://192.168.1.45:8080/zybrodental/patientApp/patientAppDeleteMultipleAppointment"

    Alamofire.request(APIMetadata.URL_DELETE_APPOINT, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case.success(let data):
            print("success",data)
             self.ArrayList.remove(at: currentIndexpath.row)
             self.yourtableViewName.deleteRows(at: [currentIndexpath], with: .fade)

        case.failure(let error):
            print("Not Success",error)
        }
    }
}

答案 1 :(得分:0)

首先,您要在myButt(_ sender: UIButton)函数中使用 Sender.tag ,然后,您必须从cellForRowAt tableView方法中分配该标签。 请使用此cellForRowAt方法。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "ACell", for: indexPath) as? ATableViewCell
     cell?.lblText.text = "\(arrIndex[indexPath.row])"
     cell?.btnDelete.tag = indexPath.row
     cell?.btnDelete.addTarget(self, action: #selector(self.myButt(_:)), for: .touchUpInside)

     return cell ?? UITableViewCell()
}

如果要在“删除”按钮操作事件中删除特定单元格,请使用此

@IBAction func myButt(_ sender: UIButton) {
    let cancelBtn = sender.tag
    let indexPath = IndexPath(row: arrIndex.index(where: {$0 == "\(cancelBtn)"}) ?? 0, section: 0)
    self.arrIndex.remove(at: sender.tag)
    tableView.deleteRows(at: [indexPath], with: .fade)
    tableView.reloadData()
}