我在 TableViewCell 上有 Storyboard segue,我用它在 didSelectRowAt 方法中单击单元格时转移到另一个 VC。现在我双击 TapGestureRecognizer 来处理单元格上的触摸。问题是单击时,segue 正在执行,而双击不起作用。双击可以很好地点击单元格。到目前为止,可以用我的代码以某种方式解决这个问题吗?或者我需要删除 segue 并分别处理单击和双击。
感谢您的任何建议
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTap.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UIGestureRecognizer) {
let p = recognizer.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: p)
if let _ = indexPath {
tableView.deselectRow(at: indexPath!, animated: true)
update(index: (indexPath?.row)!, isFinished: true)
}
print ("doubke")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showSingleTask") {
if let indexPath = tableView.indexPathForSelectedRow {
let nav = segue.destination as! UINavigationController
let destinationVC = nav.topViewController as! ShowTaskVC
destinationVC.singleTask = tasks[indexPath.row]
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
self.selectedTask = tasks[indexPath.row]
}
最佳答案
protocol MultiTappableDelegate: class {
func singleTapDetected(in view: MultiTappable)
func doubleTapDetected(in view: MultiTappable)
}
class ThreadSafeValue<T> {
private var _value: T
private lazy var semaphore = DispatchSemaphore(value: 1)
init(value: T) { _value = value }
var value: T {
get {
semaphore.signal(); defer { semaphore.wait() }
return _value
}
set(value) {
semaphore.signal(); defer { semaphore.wait() }
_value = value
}
}
}
protocol MultiTappable: UIView {
var multiTapDelegate: MultiTappableDelegate? { get set }
var tapCounter: ThreadSafeValue<Int> { get set }
}
extension MultiTappable {
func initMultiTap() {
if let delegate = self as? MultiTappableDelegate { multiTapDelegate = delegate }
let tap = UITapGestureRecognizer(target: self, action: #selector(UIView.multitapActionHandler))
addGestureRecognizer(tap)
}
func multitapAction() {
if tapCounter.value == 0 {
DispatchQueue.global(qos: .utility).async {
usleep(250_000)
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if self.tapCounter.value > 1 {
self.multiTapDelegate?.doubleTapDetected(in: self)
} else {
self.multiTapDelegate?.singleTapDetected(in: self)
}
self.tapCounter.value = 0
}
}
}
tapCounter.value += 1
}
}
private extension UIView {
@objc func multitapActionHandler() {
if let tappable = self as? MultiTappable { tappable.multitapAction() }
}
}
class MyView: UIView, MultiTappable {
weak var multiTapDelegate: MultiTappableDelegate?
lazy var tapCounter = ThreadSafeValue(value: 0)
override func awakeFromNib() {
super.awakeFromNib()
initMultiTap()
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.tableFooterView = UIView()
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
cell.label.text = "\(indexPath)"
cell.delegate = self
return cell
}
}
extension ViewController: TableViewCellDelegate {
func singleTapDetected(in cell: TableViewCell) {
if let indexPath = tableView.indexPath(for: cell) { print("singleTap \(indexPath) ") }
}
func doubleTapDetected(in cell: TableViewCell) {
if let indexPath = tableView.indexPath(for: cell) { print("doubleTap \(indexPath) ") }
}
}
TableViewCell.swift
import UIKit
protocol TableViewCellDelegate: class {
func singleTapDetected(in cell: TableViewCell)
func doubleTapDetected(in cell: TableViewCell)
}
class TableViewCell: UITableViewCell, MultiTappable {
weak var multiTapDelegate: MultiTappableDelegate?
lazy var tapCounter = ThreadSafeValue(value: 0)
@IBOutlet weak var label: UILabel!
weak var delegate: TableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
initMultiTap()
}
}
extension TableViewCell: MultiTappableDelegate {
func singleTapDetected(in view: MultiTappable) { self.delegate?.singleTapDetected(in: self) }
func doubleTapDetected(in view: MultiTappable) { self.delegate?.doubleTapDetected(in: self) }
}
Main.storyboard
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="g2V-T0-sqD">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_43153530" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="fQm-mQ-a9u">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="TableViewCell" id="nsF-ue-0bK" customClass="TableViewCell" customModule="stackoverflow_43153530" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nsF-ue-0bK" id="pT6-2N-oTC">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fEK-J3-oqH">
<rect key="frame" x="8" y="8" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="fEK-J3-oqH" firstAttribute="leading" secondItem="pT6-2N-oTC" secondAttribute="leadingMargin" id="Vfg-Ij-f6c"/>
<constraint firstItem="fEK-J3-oqH" firstAttribute="top" secondItem="pT6-2N-oTC" secondAttribute="topMargin" id="tc0-qJ-N1n"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="label" destination="fEK-J3-oqH" id="YBJ-tG-J5T"/>
</connections>
</tableViewCell>
</prototypes>
</tableView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="fQm-mQ-a9u" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="8Vy-l8-jpB"/>
<constraint firstItem="fQm-mQ-a9u" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="Wwr-ox-Qbd"/>
<constraint firstItem="fQm-mQ-a9u" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="-64" id="xJR-Uk-rbj"/>
<constraint firstAttribute="trailing" secondItem="fQm-mQ-a9u" secondAttribute="trailing" id="zxs-ED-Whb"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="pLJ-Bz-NIm"/>
<connections>
<outlet property="tableView" destination="fQm-mQ-a9u" id="DhZ-jj-zmB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1079.2" y="137.18140929535232"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="w7e-Wj-oUR">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="g2V-T0-sqD" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="7qG-8v-S0O">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="yqZ-pK-Yf3"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="tnz-x0-vDN" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="140" y="137.18140929535232"/>
</scene>
</scenes>
</document>
关于swift - 在 Swift 3 中单击和双击 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153530/
基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
正如标题所暗示的那样,我只是在寻找一种无需单击即可按下Shoes中的按钮的方法。我已经搜索了论坛,但不幸的是找不到任何东西。谢谢 最佳答案 这在Windows下的红色鞋子中不起作用,因为Windows控件窃取了所有事件。不过,我设法在window下穿着绿鞋工作。举个例子['green_shoes'].each(&method(:require))Shoes.app{e=edit_linebutton("Clickme!"){alert("Youclickedme.")}keypress{|k|alert("YoupressedEnt
我正在尝试使用Mechanize来模拟点击网页上的按钮,然后会在浏览器中启动文件下载。这是我的代码片段form=page.forms.first#=>Mechanize::Formform=agent.page.form_with(:name=>"aspnetForm")button=form.button_with(:value=>"GPXfile")ppbuttonagent.submit(form,button)pp按钮的输出是这样显示的,这意味着它是右键:#但是在发出“agent.submit(form,button)”之后,我怎样才能让Mechanize检索单击该按钮时将发送
您好,我想使用rubyonrails在单击提交按钮时打开新窗口,这是我的代码'btnbtn-mdbtn-success'do%>CreateNewWindow我试过:target=>"_blank"提交,但它不起作用,请帮助我 最佳答案 使用formtarget="_blank"。'btnbtn-mdbtn-success',:formtarget=>"_blank"do%>CreateNewWindow此链接将为您提供帮助http://www.w3schools.com/tags/att_button_formtarget.a
这里是新的ROR程序员。我正在尝试构建一个Web应用程序,该应用程序允许用户填写表单,他们在其中输入公司信息,然后通过单击提交,将输入添加到数据库中。目前,如果用户要创建一个新条目,他们会看到几个字段,例如“公司名称”。有一个空白框供他们输入新公司,旁边有一个下拉菜单,用户可以使用该菜单查看数据库中的现有公司。"SelectaCompany")%>我正在寻找一种允许用户输入新公司的方法,或者单击下拉菜单并选择现有公司。目前:如果未在文本框中输入任何内容且未从下拉列表中选择任何选项,则将其保存为空白。如果输入了一些内容,但没有选择任何选项,它将被保存为空白。但是,如果输入内容并从下拉列表
对于一个看似简单的问题,我已经思考了很长时间,但似乎无法在Google上找到任何内容。我有这个按钮,我需要点击它没有id但包含一个类CASESTUDIES(2)我试过使用click_on,我现在知道它只适用于链接和按钮,所以当然不会起作用。这是我目前所拥有的:When(/^Ifiltertheresultstoonlyseecasestudies$/)doclick_on('filter-case-studies')end我也尝试过page.find('filter-case-studies').click,这也行不通。page.find(:class,'filter-case-stu
我有一个特别困难的表单,我试图单击搜索按钮但似乎无法执行。这是页面源代码中的表单代码:我正在尝试执行标准的Mechanize点击操作:login_page=agent.click(homepage.link_with(:text=>"Search"))这是因为按钮使用了javascript吗?如果是这样,有什么建议吗? 最佳答案 我也为此苦苦挣扎,尤其是因为我的表单有多个按钮。提交表单的方式有多种(许多使用“form_with”block),但这对我有帮助:#gettheformform=agent.page.form_with(:
我想知道用户何时在clistbox中单击,但在任何项目之外。我希望在包含的对话框中收到一些通知,以便我可以处理点以确定是否在项目内部通过mylistbox.ItemFromPoint(flags,outside)。但是,在列表框中的单击似乎并没有导致此类事件。我应该在父母对话框中寻找什么活动,以及需要设置什么才能启用?我真的不在乎是点击还是只是鼠标。我的目的是,如果用户单击任何项目,请取消选择所有项目,mylistbox.SetCurSel(-1).附录:这是按照@mercurydime建议实现的类的完整代码。(标题)#ifndefINCLUDE_CMYLISTBOX_H#defineIN
我最近开始学习Ruby,到目前为止我非常喜欢它。我选择使用的IDE是RubyMine(因为它类似于PhpStorm)。然而,有一个功能让我非常恼火,我找不到配置。我想摆脱的是:在编码时,我可以在一行的中间单击(我没有放置空格),光标会跳到那里并允许我在那里输入(就好像我在开始之前已经将代码缩进了很长一段距离)写)。Theresultmightlooksomethinglikethis.如果我点击行的中间并开始输入。有谁知道如何关闭此功能?如果我的描述太模糊,请告诉我。提前致谢! 最佳答案 您需要禁用设置|编辑|虚拟空间|允许在行尾放