What can make UITableViewCell
detect only multitouch? If I tap with one finger it doesn't call didSelectRowAtIndex
.
Here is my Code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bookingSettingsCell", for: indexPath) as! SettingsTableViewCell
if indexPath.row < 3 {
cell.settingSwitch.removeFromSuperview()
}
cell.settingTitle.text = dataForSetting[indexPath.row].first
if dataForSetting[indexPath.row].last != "Pencil" {
cell.settingValue.isHidden = true
cell.settingChangable.isHidden = true
cell.settingSwitch.isHidden = false
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
DispatchQueue.main.async(execute: {
if indexPath.row < 3 {
let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "DatePicker") as! DatePickerViewController
destinationVC.modalPresentationStyle = .overCurrentContext
destinationVC.delegate = self
self.present(destinationVC, animated: true, completion: nil)
}
})
}
UITableViewCell class:
class SettingsTableViewCell: UITableViewCell {
@IBOutlet var settingTitle: UILabel!
@IBOutlet var settingChangable: UIImageView!
@IBOutlet var settingValue: UILabel!
@IBOutlet var settingSwitch: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
settingSwitch.transform = CGAffineTransform(scaleX: 0.65, y: 0.60)
}
}
You can solve this problem by adding the UIGesture recogniser delegate in your class. Add the UIGestureRecognizerDelegate in your class. Follow these steps:-
/*
Note:- Now for identify whether you are tapping on tableView or some where else you have to implement this gesture delegate like this.
//MARK:- Gesture delegates
HopeFully, it will works for you.
Thanks in Advance.
*/
I figured out that this happened because I added
UITapGestureRecognizer
to my superview so I can hide keyboard on touch. But actually still don't understand why in this case it worked with multitouch? I would suggest that my mistake should block all tap events.I think that the problem isn't in didSelectRowAt but in a UITableViewCell... are you using the constraint logic when you design the xib? Is possible that some object (label or imageView) create problem with touch... review your constraint or design your tableViewCell direct in uitableView from storyboard.
You can handle the behavior of
uitableviews's
didSelectRowAt
method after implementing the delegate methods of gesture recognizer. This prevents the undefined behavior of taps betweenuiview
andtableViewCell
and keeps the record of touches.UIGestureRecognizerDelegate method: