因此,我正在尝试为用户创建一种阻止其他用户或报告其他用户的方法。 我将LongPressGesture添加到了tableView单元格中,但是由于某些原因,它仅适用于indexPath 1及更高版本。它不会获取索引,甚至不会识别第一个单元格。有什么建议吗?
这是我的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
}
这是我的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)
}
}