Reputation: 813
I have a UITableView
and I implemented a UILongPressGestureRecognizer
to it. When you long press a cell, it calls a function called handleLongPress
.
func handleLongPress(sender:AnyObject){
}
The problem is, there are several types of UITableViewCells
in my UITableView
so I need to know what kind of cell
is long clicked. An example of my custom cell:
import UIKit
class ProfileTableViewCell: UITableViewCell {
@IBOutlet weak var ivProfile: UIImageView!
@IBOutlet weak var tvName: UILabel!
@IBOutlet weak var tvEmail: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I tried this:
if sender is ProfileTableViewCell{
print("Long Clicked!")
}
And this:
if let mType = sender as? ProfileTableViewCell{
print("Long Clicked!")
}
None of these work. How do I check the type of cell ?
Edit: This is how assign the gesture recognizer:
let cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) as! ProfileTableViewCell
longPress = UILongPressGestureRecognizer(target: self, action: #selector(ProfileTableViewController.handleLongPress(_:)))
longPress.minimumPressDuration = 0.5
longPress.delaysTouchesBegan = true
longPress.delegate = self
cell.addGestureRecognizer(longPress)
Upvotes: 0
Views: 50
Reputation: 7031
So everything else seems fine, just change your handler as such:
func handleLongPress(sender: UIGestureRecognizer) {
switch sender.view {
case is ProfileTableViewCell: print("Is a ProfileTableViewCell")
default: print("Is not.")
}
}
Upvotes: 1