0

So i'm trying to create a way for users to block other users or report them. I added the the LongPressGesture to my tableView cells, but for some reason, it only works from indexPath 1 and above. It won't grab the index or even recognize the first cell. Any suggestions?

This is my cellForRowAt

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
    let userID = posts[indexPath.row].userid
    let userDBRef = Database.database().reference().child("users/\(userID)")
    userDBRef.observe(DataEventType.value) { (snapshot) in
        let user = Users(snapshot: snapshot)
        cell.usernameLabel.setTitle(user.username, for: .normal)
        cell.userProfilePic.sd_setImage(with: URL(string: user.photoURL!), placeholderImage: UIImage(named: "1"))
    }




    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureReconizer:)))
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    lpgr.delegate = self
    cell.addGestureRecognizer(lpgr)
    if self.blockedUsersArray.contains("\(posts[indexPath.row].userid)") {
        print("blocked user found")
        self.posts.remove(at: indexPath.row)
        self.tableView.reloadData()
    } else {
        print("no blocked users")
    }

    cell.postDescriptionLabel.text = posts[indexPath.row].description
    cell.postTitleLabel.text = posts[indexPath.row].title
    cell.postTitleLabel.amx_autoScaleFont(forReferenceScreenSize: .size3p5Inch)
    cell.postTitleLabel.amx_autoScaleFont(forReferenceScreenSize: .size4Inch)
    cell.postTitleLabel.amx_autoScaleFont(forReferenceScreenSize: .size4p7Inch)
    cell.postTitleLabel.amx_autoScaleFont(forReferenceScreenSize: .size5p5Inch)
    let image = posts[indexPath.row]
    cell.postImageView.sd_setImage(with: URL(string: image.postImageStringUrl), placeholderImage: UIImage(named: "1"))

    cell.delegate = self


    return cell


}

This is my handleLongPress()

@objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
    if gestureReconizer.state != UIGestureRecognizerState.began { }
    let p = gestureReconizer.location(in: tableView)
    let indexPath = tableView.indexPathForRow(at: p)
    if Auth.auth().currentUser != nil {
        if let index = indexPath {
            var cell = tableView.cellForRow(at: index)
            let userID = posts[(indexPath?.row)!].userid
            let blockedUser = posts[(indexPath?.row)!].username
            self.userToBlock = userID
            print(userID)
            self.userNameToBlock = blockedUser
            let myUserID = Auth.auth().currentUser!.uid

            let alercontroller = UIAlertController(title: "", message: "Please choose one of the following", preferredStyle: .alert)
            let blockAction = UIAlertAction(title: "Block User", style: .destructive) { (action) in
                print("Block" + self.userToBlock!)
                self.profileDBRef?.child("users/" + self.userToBlock! ).child("BlockedUsers").child(myUserID).child(myUserID).setValue(Auth.auth().currentUser?.displayName!)
                self.profileDBRef?.child("users/" + myUserID).child("BlockedUsers").child(userID).child(userID).setValue(self.userNameToBlock)
                self.loadBlockedUser()
                self.tableView.reloadData()

            }
            print(blockedUsersArray)
            let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            alercontroller.addAction(blockAction)
            alercontroller.addAction(cancel)
            self.present(alercontroller, animated: true, completion: nil)
            // do stuff with your cell, for example print the indexPath
            print(index.row)
        } else {
            print("Could not find index path")
        }
    } else {
        let alertcontroller = UIAlertController(title: "Please log in", message: "Must Be a user to report or block a user", preferredStyle: .alert)
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertcontroller.addAction(cancel)
        self.present(alertcontroller, animated: true, completion: nil)
    }


}
Adrian Navarro
  • 135
  • 2
  • 10
  • You are setting gesture recognizer delegate property. Are you possibly implementing some of its methods which may alter the way gesture is handled, such as `gestureRecognizerShouldBegin`, etc.? You're also only adding the recognizer, never checking if the cell already has one. When your cell gets reused you are possibly adding multiple recognizers to the same cell, which may result in strange behaviour. – Au Ris Jul 05 '18 at 09:47
  • Also why do you have a line of code which does nothing: `if gestureReconizer.state != UIGestureRecognizerState.began { }` – Au Ris Jul 05 '18 at 09:55

0 Answers0