-1

I am adding a custom swipe gesture on a UITableViewCell that updates CoreData when a specific cell is swiped. However, I am having trouble passing the indexPath of the swiped cell to the function that will modify the CoreData. I am not using a Table View Swipe Action - I am animating the cell when it is swiped to cross the item out, which means I will need to call an animation on the custom UITableViewCell. So far, I have tried passing the indexPath of the swiped cell to the #selector of the UISwipeGestureRecognizer like so:

The function that handles the gesture:

    @objc func handleSwipeGesture(sender: UISwipeGestureRecognizer ,at indexPath: IndexPath) {
    itemArray[indexPath.row].complete = !itemArray[indexPath.row].complete
    print("\(itemArray[indexPath.row].complete)")
    saveItems()
//      call animation??
    }

In cellForRowAt in the UITableViewDelegate I declare the swipe action and pass the indexPath of the swiped cell as a parameter to the function above.

    let swipeToComplete = SwipeCompletion.init(target: self, action: #selector(handleSwipeGesture))
    swipeToComplete.indexPath = indexPath
    cell.addGestureRecognizer(swipeToComplete)

This is the class that enables me to pass the indexPath through:

class SwipeCompletion: UISwipeGestureRecognizer {
var indexPath: IndexPath!
}

However, this I get the error when I swipe on a cell: EXC_BAD_ACCESS (code=1, address=0xf)

Any ideas on how I could achieve this and how I could call the cell animation on a cell?

Maulik Pandya
  • 2,200
  • 17
  • 26
gmdev
  • 2,725
  • 2
  • 13
  • 28
  • 3
    *I ... pass the indexPath of the swiped cell as a parameter*. No you don't. It's impossible to pass custom parameters in target/action selectors. Declare the gesture in the cell and add a callback closure which passes the index path to the cell itself. Nevertheless why don't you use the built-in swipe actions? – vadian Dec 10 '19 at 12:00
  • @vadian I am not using the built-in swipe actions because I am animating a UIView in the custom table view cell when the cell is swiped. – gmdev Dec 10 '19 at 12:55

1 Answers1

0

On these "#selectors" you can't customize the values passed to the call (As far as I know).

It looks like you have indexPath as a property of your custom recognizer.

Use that indexPath, not the one being passed in. Thats likely not the correct instance.

Something like this might be what your trying to do.

    @objc func handleSwipeGesture(sender: SwipeCompletion) {
       itemArray[sender.indexPath.row].complete = !itemArray[sender.indexPath.row].complete
       print("\(itemArray[sender.indexPath.row].complete)")
       saveItems()
      //call animation??
    }
Tofu Warrior
  • 386
  • 3
  • 10