Hmm, better use alert control in the view controller, because in controller, u can get every thing like tableview (say after deleting comment u have to reload), the data to be delete (something which is present in the (for example)array that u are used to display in tableview)... etc
first define a delegate method in the cell file
for example
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
}
in the 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()
}
}