确认删除 Table 视图单元格中的 post?

Confirmation for deleting a post in Table View Cell?

我有一个 UItableview,其中有显示 post 用户的单元格。

我希望用户能够使用 post 中显示的 "delete button" 删除他们的 post。

我可以做到,但我希望用户在单击单元格中的删除按钮时首先弹出一个确认窗口。

所以我将下面的代码作为操作添加到 table 的 "cell" 文件中,但我收到一个错误 "use of unresolved identifier presentviewcontroller"。

我不能在单元格文件中使用 presentviewcontroller 吗?

@IBAction func button_clicked(sender: AnyObject) {
    var refreshAlert = UIAlertController(title: "Refresh", message: "Do you want to delete this post?", preferredStyle: UIAlertControllerStyle.Alert)

    refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
        println("Handle Ok logic here")
    }))

    refreshAlert.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
        println("Handle Cancel Logic here")
    }))

    presentViewController(refreshAlert, self, completion: nil) 
}

不知道你能不能做到。但你绝对不应该这样做。在视图 class 中编写 'controller' 代码绝对不是正确的做法。

如果我是你,我会做这样的事情。

按下行中的删除按钮后,应为行的 indexPath 调用 UITableViewController 中的函数。

您的 confirmationToDeleteIndexPath 函数应该显示 refreshAlert 并询问用户。在其回调中,您应该尝试删除之前请求的 indexPath。

语法是这样的:

self.presentViewController(<viewControllerToPresent: UIViewController>, animated: <Bool>, completion: <(() -> Void)?() -> Void>)

所以,你需要做这样的事情:

self.presentViewController(refreshAlert, animated: true, completion: nil)

嗯,最好在视图控制器中使用警报控件,因为在控制器中,你可以获得所有东西,比如表视图(比如删除评论后你必须重新加载),要删除的数据(存在于(例如)你用来在表格视图中显示的数组)...等等

首先在cell file中定义一个委托方法,例如

 import UIKit
 @objc protocol CellDelegate
 {
     func deleteCell(cell:CustomCommentCell)
 }
 class CustomCommentCell: UITableViewCell {
 @IBOutlet weak var deleteButton: UIButton!  //a delete button
 weak var delegate:CellDelegate?   //declare a delegate 

  override init(style: UITableViewCellStyle, reuseIdentifier: String?)
 {
     super.init(style: style, reuseIdentifier: reuseIdentifier)     
 }

 required init(coder aDecoder: NSCoder)
 {
    super.init(coder: aDecoder)
 }

 override func awakeFromNib() {
     super.awakeFromNib()
     // Initialization code
 }

 //other code
 //......
 @IBAction func button_clicked(sender: AnyObject)
 {
    self.delegate?.deleteCell(self) //call the delegat method
 }

ViewController

import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate, CellDelegate,UIAlertViewDelegate // add `CellDelegate`,UIAlertViewDelegate if u want  t use alert view
{
   //...other code
   // ....

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        var cell:CustomCommentCell? = tableView.dequeueReusableCellWithIdentifier("CELL") as? CustomCommentCell;
       if(cell == nil)
       {
        //..cell initilise
       }
       cell?.delegate = self  //set the delegate to self
       //..other code set up comment string .. etc
       return cell!;
   }

   //define custom delegate method hear u can delete the cell
   //since u are passing the cell so u can get the index path of the cell 
   func deleteCell(cell: CustomCommentCell)
   {
    var deleteCell:CustomCommentCell? = cell as CustomCommentCell
    var indexPath: NSIndexPath  = self.tableView.indexPathForCell(deleteCell!)! //get the index path
    //using alert view
    var alertToDelete: UIAlertView = UIAlertView(title: "Delete", message: "Are u sure to delete this comment", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Ok")
    alertToDelete.show()

    /*  uncomment if u want to use alertControl and comment above 2 line of alertView
    //using alert control 
    var refreshAlert = UIAlertController(title: "Refresh", message: "Do you want to delete this post?", preferredStyle: UIAlertControllerStyle.Alert)
    refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
        println("Handle Ok logic here")
        //after deleting from datasource
        self.tableView.reloadData()
    }))

    refreshAlert.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
        println("Handle Cancel Logic here")
    }))

    self.presentViewController(refreshAlert, animated: true, completion: nil)
    */

  }

//suppose if u use alert view u will get delegate call back in this check which button is clicked 
 func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    if(buttonIndex == alertView.cancelButtonIndex)
    {
        //do nothing
        println("Handle Cancel Logic here")
    }
    else
    {
        //delete hear
        println("Handle Ok logic here")
        //after deleting form datasource
        self.tableView.reloadData()
    }
}

这是我以前使用过的解决方案 - 当用户在 table 中的记录上滑动时,他们会看到删除键。选择后,他们会收到一个弹出窗口,询问他们是否要确认删除。

有很多不同的方法可以实现这一点,可能是更好的方法,但希望这对某人有所帮助。

在包含 table 的视图控制器中,我包含了以下代码:

// Code to handle delete records in tableview.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {

        let uiAlert = UIAlertController(title: "Delete Record", message: "Are you sure you want to delete this record?", preferredStyle: UIAlertControllerStyle.Alert)
        self.presentViewController(uiAlert, animated: true, completion: nil)

        uiAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { action in
            //remove from data source
            self.managedObjectContext.deleteObject(self.fetchedResults[indexPath.row] as! NSManagedObject)
            do {
                try self.managedObjectContext.save()
            } catch _ {
            }

            // refresh table & header
            self.fetchData()
        }))

        uiAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

    }
}
let refreshAlert = UIAlertController(title: "Refresh", message: "Do you want to delete this post?", preferredStyle: UIAlertControllerStyle.alert)

refreshAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
    //perform your action
}))

refreshAlert.addAction(UIAlertAction(title: "No", style: .default, handler: { (action: UIAlertAction!) in
    //perform your action
}))

present(refreshAlert, animated: true, completion: nil)