【问题标题】:didSelectRowAt called only on multitouchdidSelectRowAt 仅在多点触控时调用
【发布时间】:2016-10-01 12:03:38
【问题描述】:

什么可以让UITableViewCell 只检测到多点触控?如果我用一根手指点击它不会调用didSelectRowAtIndex

这是我的代码:

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

【问题讨论】:

  • 为什么将代码放在 DispatchQueue.main.async 中?删除它并尝试。
  • 我已经添加了它,因为如果我不这样做,它会在提交 VC 之前有很长的延迟(我很久以前就找到了这个解决方案)并且 100% 确定这不是真正的问题。
  • 在我的情况下,相同的代码运行良好
  • 你的意思是双击单元格..?
  • 是的,有可能,但您必须实现手势识别器,这可能会导致禁用didSelectRowAtIndexPath 方法..

标签: ios swift uitableview didselectrowatindexpath


【解决方案1】:

您可以在实现手势识别器的委托方法后处理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
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我认为问题不在于 didSelectRowAt,而在于 UITableViewCell... 设计 xib 时是否使用约束逻辑? 某些对象(标签或图像视图)可能会产生触摸问题... 查看您的约束或从情节提要直接在 uitableView 中设计您的 tableViewCell。

      【讨论】:

      • 我在故事板中设计了它。问题是,据我所知,它应该检测到触摸(一切正常)或不检测(某些视图覆盖等),但它只检测到多点触控,这很奇怪。
      【解决方案4】:

      您可以通过在您的类中添加 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
      }
      

      HopeFully,它对你有用。

      提前致谢。

      */

      【讨论】:

        【解决方案5】:

        您可以通过删除视图的所有手势来检查是否由于任何手势而发生,如下所示并尝试。

        view.gestureRecognizers?.removeAll()
        

        最好找到导致问题的确切手势并正确处理。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多