2016-10-01 85 views
1

什么可以使UITableViewCell仅检测多点触控?如果我用一个手指点击它不会叫didSelectRowAtIndexdidSelectRowAt仅在多点触控上调用

这里是我的代码:

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

为什么你把你的代码放进DispatchQueue.main.async? 删除它并尝试。 –

+0

我已经添加了它,因为如果我在提交VC(我很久以前发现这个解决方案)之前没有很长的延迟,并且100%确定它不是真正的问题。 – JuicyFruit

+0

相同的代码在我的情况下工作正常 –

回答

2

在实现手势识别器的委托方法后,您可以处理uitableviews'sdidSelectRowAt方法的行为。这可以防止uiviewtableViewCell之间的未定义行为,并保持触摸记录。

UIGestureRecognizerDelegate方法:

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

我觉得这个问题是不是在didSelectRowAt但在一个UITableViewCell ... 是您在使用设计xib时的约束逻辑? 有可能是一些对象(标签或imageView)触摸产生问题... 审查您的约束或设计您的tableViewCell直接在故事板uitableView。

+0

我在故事板中设计了它。问题是,据我所知,它应该检测触摸(全部是好的)还是不检测(某些视图覆盖等),但它只检测多点触摸,这很奇怪。 – JuicyFruit

0

我发现这是因为我在我的超级视图中添加了UITapGestureRecognizer,所以我可以隐藏键盘。但实际上仍然不明白为什么在这种情况下它使用多点触控?我会建议我的错误应该阻止所有点击事件。

0

你可以在你的类添加UIGesture识别器代表解决这个问题。在您的班级中添加UIGestureRecognizerDelegate。遵循以下步骤: -

/*

class SignupInstructorViewController: UIViewController, UIGestureRecognizerDelegate 

注: -现在对于确定您是否正在敲击的tableView或者一些别的地方,你必须实现这个手势代表这样。

// MARK: - 手势代表

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 
} 

希望,它会为你的作品。

在此先感谢。

*/