Ios 如何检测UITableView上的触摸,到目前为止似乎什么都不起作用?

Ios 如何检测UITableView上的触摸,到目前为止似乎什么都不起作用?,ios,uitableview,swift,touch,Ios,Uitableview,Swift,Touch,如何在UITableView上检测触摸?我尝试了这方面的解决方案。我的tableView和scrollView在同一个UIViewController中,有一个约束,因此对tableView进行子类化非常复杂 不调用以下方法: class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var verti

如何在
UITableView
上检测触摸?我尝试了这方面的解决方案。我的
tableView
scrollView
在同一个
UIViewController
中,有一个约束,因此对
tableView
进行子类化非常复杂
不调用以下方法:

class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var verticalSpacing: NSLayoutConstraint!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var tableView: UITableView!


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)
    println("touch began")
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    super.touchesMoved(touches, withEvent: event)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    super.touchesEnded(touches, withEvent: event)
    println("touch ended")
}
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
    super.touchesCancelled(touches, withEvent: event)
}
我补充说:

tableView.userInteractionEnabled = true
tableView.canCancelContentTouches = false //or true is the same

但这并没有改变任何事情。
滚动视图
表格视图
同时可见,但它们不重叠。

您的问题是,
触摸开始
等也是
UIView
中的方法,因此您需要对
UITableView
进行子类化,或者使用TapGestureRecognitor使其按需要工作


UITableView
进行子类化应该不会太复杂,因为您可以在故事板中将UITableView类设置为tableView的类,就像对uiViewController等所做的那样。

您的问题是,
touchsbegind
等也是来自
UIView
的方法,因此您需要对
UITableView
进行子类化,或者使用TapGestureRecognitor使其正常工作


UITableView
子类化应该不会太复杂,因为您可以在情节提要中将UITableView类设置为tableView的类,就像使用uiViewController等一样。

在使用检测滚动视图更改的UITableView时,有一些方法非常有用。这些可能对你有用

比如说,

override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    println("Ended decelerating")
}

override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    println("Ended dragging")
}

使用UITableView检测对scrollview的更改时,有一些方法非常有用。这些可能对你有用

比如说,

override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    println("Ended decelerating")
}

override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    println("Ended dragging")
}

您是否尝试过调整
延迟内容切换
?它可能是,如果你按下足够长的时间,你的触摸将登记默认延迟


必须问一下,您是否有理由不能直接使用
didSelectRowAtIndexPath

您是否尝试过调整
延迟内容切换
?它可能是,如果你按下足够长的时间,你的触摸将登记默认延迟


必须问一下,有什么原因不能直接使用
didSelectRowAtIndexPath

不需要子类化

只需添加一个手势识别器,并确保包括对
cancelsTouchesInView

以下是Swift版本:

override func viewDidLoad() {
    super.viewDidLoad()
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        // Do your thang here!
    }
    sender.cancelsTouchesInView = false
}

享受吧

不需要子类化

只需添加一个手势识别器,并确保包括对
cancelsTouchesInView

以下是Swift版本:

override func viewDidLoad() {
    super.viewDidLoad()
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        // Do your thang here!
    }
    sender.cancelsTouchesInView = false
}

享受吧

派对迟到了,但我需要检测触摸而不是在桌子上轻触,所以我结束了这个:

fileprivate class TouchSensitiveTableView: UITableView {

    fileprivate var onTouchStart: (() -> ())? = nil
    fileprivate var onTouchEnd: (() -> ())? = nil

    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.onTouchStart?()
    }

    public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.onTouchEnd?()
    }
}

与桌子的滚动有轻微的互动,但没有什么大的。

晚会迟到了,但我需要检测触摸而不是在桌子上轻触,因此我总结如下:

fileprivate class TouchSensitiveTableView: UITableView {

    fileprivate var onTouchStart: (() -> ())? = nil
    fileprivate var onTouchEnd: (() -> ())? = nil

    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.onTouchStart?()
    }

    public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.onTouchEnd?()
    }
}

与表格的滚动有轻微的交互,但无重大影响。

谢谢,我将使用willBeginDragging和DiEndDraging,谢谢回答谢谢,我将使用willBeginDragging和DiEndDraging,谢谢回答谢谢,我将继续使用willBeginDragging和DiEndDraging,它们似乎很有效。当用户停止拖动时,我必须检查滚动并取消它。谢谢你的回答谢谢,我会和你的遗嘱呆在一起,这看起来很有效。当用户停止拖动时,我必须检查滚动并取消它。感谢您的回答。UtableView中的点击使用此解决方案停止…UtableView中的点击使用此解决方案停止。。。