确认删除表视图单元格中的帖子?

时间:2015-06-09 08:30:54

标签: uitableview swift presentviewcontroller

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

我希望用户能够使用"删除按钮"删除他们的帖子。在他们的帖子中显示。

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

所以我将下面的代码添加为" cell"该表的文件,但我得到一个错误,说"使用未解析的标识符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) 
}

5 个答案:

答案 0 :(得分:1)

语法是:

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

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

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

答案 1 :(得分:0)

我不确定你能不能做到。但你绝对不应该这样做。写一个&#39;控制器&#39;视图类中的代码绝对不是这样做的。

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

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

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

答案 2 :(得分:0)

嗯,最好在视图控制器中使用警报控制,因为在控制器中,你可以得到像tableview这样的东西(比如说删除注释后你必须重新加载),要删除的数据(存在于(用于)例子)用于在tableview中显示的数组)... etc

首先在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()
    }
}

答案 3 :(得分:0)

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

实现这一目标的方法很多,可能是更好的方法,但希望这有助于某人。

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

// 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))

    }
}

答案 4 :(得分:0)

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)