Ios 滑动以删除UITableViewCell
我尝试在表视图单元格上执行“滑动以删除”,但在多次滑动该单元格后,只有该单元格被删除。如何使其顺利工作(在一次尝试刷和删除)?我的代码在这里:Ios 滑动以删除UITableViewCell,ios,swift,Ios,Swift,我尝试在表视图单元格上执行“滑动以删除”,但在多次滑动该单元格后,只有该单元格被删除。如何使其顺利工作(在一次尝试刷和删除)?我的代码在这里: func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { var bId = Helper.businessId as String let par
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
var bId = Helper.businessId as String
let param = ["id" : bId] as! [String : AnyObject]
if editingStyle == .delete{
print(Helper.memId)
print("Deleted")
var myDictionary = self.List[indexPath.row] as! NSDictionary
var bId : String!
if myDictionary.allKeys.count>0
{
bId = myDictionary["id"] as! String
}
var bb = bId as String
if editingStyle == .delete{
print(Helper.memId)
print("Deleted")
Alamofire.request("http://api.noattabeta.com/api/v1/user/current/memberships/\(bb)", method: .delete, headers: ["Authorization":Helper.token]).responseString
{response in
print("In Response")
switch response.result {
case .success:
DispatchQueue.main.async {
let myAlert = UIAlertController(title:"Alert", message: "Do You want to delete",preferredStyle:UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title:"Ok",style:UIAlertActionStyle.default,handler:{ (action: UIAlertAction!) in
self.tableView.reloadData()
})
myAlert.addAction(okAction)
let cancelAction = UIAlertAction(title:"Cancel",style:UIAlertActionStyle.default,handler:{ (action: UIAlertAction!) in
self.tableView.reloadData()
})
myAlert.addAction(okAction)
myAlert.addAction(cancelAction)
self.present(myAlert, animated: true, completion: nil)
}
case .failure(let error):
print(error)
}}}
首先,我想指出,您在错误的位置发布警报。在API成功后,您对其进行删除调用,询问他或她是否要确认。你必须在打电话删除之前询问它
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
var bId = Helper.businessId as String
let param = ["id" : bId] as! [String : AnyObject]
if editingStyle == .delete{
print(Helper.memId)
print("Deleted")
var myDictionary = self.List[indexPath.row] as! NSDictionary
var bId : String!
if myDictionary.allKeys.count>0
{
bId = myDictionary["id"] as! String
}
var bb = bId as String
if editingStyle == .delete{
print(Helper.memId)
print("Deleted")
let myAlert = UIAlertController(title:"Alert", message: "Do You want to delete",preferredStyle:UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title:"Ok",style:UIAlertActionStyle.default,handler:{ (action: UIAlertAction!) in
// here you can call the delete if user accepts
Alamofire.request("http://api.noattabeta.com/api/v1/user/current/memberships/\(bb)", method: .delete, headers: ["Authorization":Helper.token]).responseString
{response in
print("In Response")
switch response.result {
case .success:
DispatchQueue.main.async {
// remove from your local array e.g
self.dataModel.remove(at: indexPath.row)
// Delete the row before you reload the tableView
self.tableView.deleteRows(at: [indexPath], with: .automatic)
// Now reload your tableView
self.tableView.reloadData()
}
case .failure(let error):
print(error)
})
myAlert.addAction(okAction)
let cancelAction = UIAlertAction(title:"Cancel",style:UIAlertActionStyle.default,handler:{ (action: UIAlertAction!) in
self.tableView.reloadData()
})
myAlert.addAction(okAction)
myAlert.addAction(cancelAction)
self.present(myAlert, animated: true, completion: nil)
}
您需要等待服务器响应,同时显示加载程序,以便用户知道删除正在进行中。