uitableview - 确认删除 TableView 单元格中的帖子?

标签 uitableview swift presentviewcontroller

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

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

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

因此,我将下面的代码添加为表的“单元格”文件中的操作,但出现错误,提示“使用未解析的标识符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) 
}

最佳答案

嗯,最好在 View Controller 中使用警报控件,因为在 Controller 中,您可以获取诸如 TableView 之类的所有内容(例如删除注释后您必须重新加载),要删除的数据(存在于(对于示例)用于在 TableView 中显示的数组)...等

首先在单元文件中定义一个委托(delegate)方法,例如

 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()
    }
}

关于uitableview - 确认删除 TableView 单元格中的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726813/

相关文章:

iphone - 如果表为空,cellForRowAtIndexPath 应该返回什么?

ios - TableView 单元格不显示更新的数据

ios - 在 iOS 应用程序中播放 Youtube 视频

swift - 快速从另一个 View Controller 呈现 View Controller 的问题

ios - splitViewController showDetailViewController 没有动画?

ios - 如何删除 UITableView 中的空单元格?

iphone - tableView 滚动时 UITableView 委托(delegate)操作?

ios - UIAlertController swift

ios - 如何处理 Swift 中的竞争条件读/写问题?

ios - 呈现 View Controller 上的模态视图 Controller