didSelectRowAt called only on multitouch

2019-06-03 08:53发布

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

4条回答
一纸荒年 Trace。
2楼-- · 2019-06-03 09:03

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.

*/

查看更多
疯言疯语
3楼-- · 2019-06-03 09:07

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楼-- · 2019-06-03 09:16

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.

查看更多
时光不老,我们不散
5楼-- · 2019-06-03 09:18

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
    }
}
查看更多
登录 后发表回答