0

Basically, I have a custom table view cell that lists potential employees. This cell includes a few labels and a button. What I want is for the button to remove the cell, but all I can find is this :

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
  numbers.removeAtIndex(indexPath.row)    
  tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}

}

Which only allows you to delete the cell if you swipe then choose "delete"

I'm thinking I should create a function in my tableViewController.swift file that will delete the cell at a given row, and in my custom cell class create an IBAction that will call said function.

What would the function to delete a cell at given row look like? But in this case, does the cell also know which row it is in or is that a job for the tableViewController?

mir-aclewhip
  • 127
  • 2
  • 11
  • See this: http://stackoverflow.com/questions/8983094/how-to-enable-swipe-to-delete-cell-in-a-tableview – koen Mar 31 '16 at 22:59

3 Answers3

1
  1. In your cellForRowAtIndexPath set the cell.button.tag = indexPath.row

  2. Add a target to the button: cell.button addTarget:...

  3. In the button method, remove the item in your dataArray at index button.tag. Then refresh the tableView self.tableView reloadData

Peter
  • 1,053
  • 13
  • 29
1

You can use the tag property of UIButton to store the index of the cell that you want to delete and use that tag property in the handler to locate the correct cell. In my sample code, I just made an assumption that you only have one section that is why it is set to zero.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
  ...
  ...
  cell.button.tag = indexPath.row
  let tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleDeleteTap:"))
  cell.button.addGestureRecognizer(tapGesture)

}

func handleDeleteTap(sender: UITapGestureRecognizer)
{
  if let button = sender.view as? UIButton
  {
    let indexPath = NSIndexPath(forRow: button.tag, inSection: 0)
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
  }
}
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
1

1) You have to define an action for your button in cellForRowAtIndexPath:

...
myButton.addTarget(self, action: "buttonTappedDelete:", forControlEvents: .TouchUpInside)
...

2) You have to implement the selector:
2.1) Get the cell in which the button is located.
2.2) Get the index path of the cell.
2.3) Remove the cell from the table view and update your data.

func buttonTappedDelete(sender: UIButton) {
    let cell = sender.superview!.superview as! UITableViewCell
    let indexPath = self.tableView.indexPathForCell(cell)!
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    // update your data model, call `self.tableView.reloadData()`
}
borchero
  • 5,562
  • 8
  • 46
  • 72