ios - 通过单击单元格中的取消按钮来删除该单元格,也可以通过 editActionsForRowAt 方法删除该单元格

标签 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 工作正常...

最佳答案

为 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]
}

最终创建delete 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)
        }
    }
}

关于ios - 通过单击单元格中的取消按钮来删除该单元格,也可以通过 editActionsForRowAt 方法删除该单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569065/

相关文章:

ios - 在 SpriteKit 中随着时间的推移减小 CGPath 的大小?

Swift - 将结构传递给方法?

iphone - UITableView footerView 带按钮,按钮不起作用

ios - 从自定义 Cell 中正确委托(delegate)按钮操作以删除 UITableView 中的行

ios - 将发件人 : Any? 与 UIBarButtonItem 进行比较

ios - Swift:让省略号不断地在标签中重新输入?硬标签 "animation"?

ios - Xcode 卡在 'waiting for other symbol processing'

ios - 如何在 Swift 中写入 Firebase ref 的最后一个 child ?

ios - iOS应用启动时如何区分通知是本地通知还是远程通知

ios - iOS 9 阻止与以编程方式创建的 UISearchController 进行交互