didSelectRowAt called only on multitouch

℡╲_俬逩灬. 提交于 2019-12-10 11:34:21

问题


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)
    }
}

回答1:


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 between uiview and tableViewCell and keeps the record of touches.

UIGestureRecognizerDelegate method:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    println("in shouldReceiveTouch")
    gestureRecognizer.delegate = self
    if (touch.view == tableView){
        println("touching tableView list")
        return false
    }
    else{
        println("touching elsewhere")
        return true
    }
}



回答2:


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.




回答3:


I figured out that this happened because I added UITapGestureRecognizerto 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.




回答4:


You can solve this problem by adding the UIGesture recogniser delegate in your class. Add the UIGestureRecognizerDelegate in your class. Follow these steps:-

/*

class SignupInstructorViewController: UIViewController, UIGestureRecognizerDelegate

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

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    // if user touch the tableview then below condition will be true
    if touch.view != nil && (touch.view!.isDescendantOfView(tableViewName)) {
        return false
    }
    return true
}

HopeFully, it will works for you.

Thanks in Advance.

*/



来源:https://stackoverflow.com/questions/39806403/didselectrowat-called-only-on-multitouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!