-1

I'm using a UISwipeGestureRecognizer to detect a swipe for a cell in a UITableViewCell, similar to THIS LINK which will allow the user to 'Like' a photo.

The problem is that I dont quite understand how to change the Like value for that specific post - and it doesn't have an indexPath like other 'built-in' methods. I also don't understand how it knows to use the cell that is showing predominantly on the screen, since there might be more than one cell that has not yet been "dequeued"?:

@objc func mySwipeAction (swipe: UISwipeGestureRecognizer) {

    switch swipe.direction.rawValue {
    case 1:
        print ("the PostID you selected to LIKE is ...")

    case 2:
          print ("the PostID you selected to Undo your LIKE is ...")

    default:
        break
    }
}

and my tableView looks like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "postTopContributions", for: indexPath) as! PostTopContributions
    let postImage = postImageArray [indexPath.row]
    let imageURL = postImage.postImageURL

    cell.delegate = self

    cell.postSingleImage.loadImageUsingCacheWithUrlString(imageURL)
    cell.postSingleLikes.text = "\(postImageArray [indexPath.row].contributionPhotoLikes)"
    cell.postSingleImage.isUserInteractionEnabled = true


    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(self.mySwipeAction(swipe:)))
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(self.mySwipeAction(swipe:)))

    leftSwipe.direction = UISwipeGestureRecognizerDirection.left
    rightSwipe.direction = UISwipeGestureRecognizerDirection.right

    cell.postSingleImage.addGestureRecognizer(leftSwipe)
    cell.postSingleImage.addGestureRecognizer(rightSwipe)

    let selectedCell = self.postImageArray [indexPath.row]

    return cell
}

I don't want to use the native TableView row swipe left to delete methods - for various UX purposes in this specific case.

Brewski
  • 654
  • 3
  • 14
  • 29

3 Answers3

1

You can try

cell.postSingleImage.addGestureRecognizer(leftSwipe)
cell.postSingleImage.addGestureRecognizer(rightSwipe)
cell.postSingleImage.tag = indexPath.row

Don't recommend adding gestures inside cellForRowAt , you may add them inside init for programmatic cells or awakeFromNib for xib / prototype cells


@objc func mySwipeAction (swipe: UISwipeGestureRecognizer) {

    let index = swipe.view.tag
    let selectedCell = self.postImageArray[index]
    switch swipe.direction.rawValue {
    case 1:
        print ("the PostID you selected to LIKE is ...")
       // edit dataSource array
    case 2:
          print ("the PostID you selected to Undo your LIKE is ...")
       // edit dataSource array

    default:
        break

   // reload table IndexPath
    }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

You can pass your indexpath as a parameter in your selector. and then add the like at yourArray[indexpath.row]

shayegh
  • 302
  • 1
  • 9
0

You can set the tag of the cell image that you are adding the GestureRecognizer to the indexPath row of the cell itself:

cell.postSingleImage.tag = indexPath.row
cell.postSingleImage.addGestureRecognizer(leftSwipe)
cell.postSingleImage.addGestureRecognizer(rightSwipe)

Then, you can determine which cell triggered the GestureRecognizer by getting the view's tag that triggered the swipe gesture:

@objc func mySwipeAction (gesture: UISwipeGestureRecognizer) {
     let indexPathRow = gesture.view.tag
     let indexPath = IndexPath(row: indexPathRow, section: 0) // assuming this is a 1 column table not a collection view
     if let cell = tableView.cellForRow(at: indexPath) as? PostTopContributions {
          // ... and then do what you would like with the PostTopContributions cell object
          print ("the PostID you selected to LIKE is ... " + cell.id)

     }

}

Hope that this helped!

  • Thanks but I can't get this to work: Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)' – Brewski Nov 29 '18 at 06:12
  • That means that Swift is not finding your `tableView`. Make sure that your `tableView` is declared in your `UIViewController` and your `UIViewController` is a `UITableViewDataSource` and `UITableViewDelegate`. Check out this post on SO: https://stackoverflow.com/questions/33724190/ambiguous-reference-to-member-tableview – hackerman58888 Nov 29 '18 at 14:27