使用 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/