UITableViewCell 内的 UIScrollView 触摸检测 [英] UIScrollView inside UITableViewCell touch detect
问题描述
我有一个带有 8 个自定义单元格的 tableview.在第 8 个单元格中,我添加了一个启用分页的 scrollView,这样我就可以在没有非常高的单元格的情况下显示第 1 页和第 2 页(或第 3、4...10 页).
I have a tableview with 8 custom cells. in the 8th cell I added a scrollView with paging enabled so I can show page 1 and page 2 (or 3, 4... 10) without have a very high cell.
问题出在 scrollView 我不能使用 didSelectRowAtIndexPath 因为单元格在 scrollView 后面所以我试图检测 scrollView 点击(不是滑动).
The problem is with the scrollView I can't use didSelectRowAtIndexPath because the cell is behind the scrollView so I'm trying to detect scrollView tap (not swipe).
我玩过 touchesBegan 和 touchesEnded 但他们从来没有被调用过(我知道 touches 只适用于 UIView,但也许......)
I played with touchesBegan and touchesEnded but they are never called (I know touches work with UIView only, but maybe.....)
非常感谢任何帮助.
谢谢,最大
推荐答案
我找到了最简单的解决方案:
I found the simplest solution for my needs:
继承 UIScrollView touchesEnded 方法并发布通知.
subclass UIScrollView touchesEnded method and post a notification.
在 UITableview 中添加一个观察者在 viewdidAppear 中(在 viewdiddisappear 中移除它)来调用一个调用 tableview didSelectRowForIndexPath 的函数.
In the UITableview add an observer in viewdidAppear (remove it in viewdiddisappear) to call a function that call tableview didSelectRowForIndexPath.
类似的东西(快速版本)
Something like this (swift version)
// myScrollView.swift
import UIKit
class myScrollView: UIScrollView {
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
NSNotificationCenter.defaultCenter().postNotificationName("selectTVRow", object: nil)
}
}
在您的 tableView 中:
In your tableView:
// ItemsList.swift
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectFourthRow", name: "selectTVRow", object: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "selectfourthrow", object: nil)
}
func selectFourthRow() {
let rowToSelect:NSIndexPath = NSIndexPath(forRow: 4, inSection: 0);
self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect);
}
/*
.... rest of your tableview Datasource and Delegate methods...
numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath
*/
这篇关于UITableViewCell 内的 UIScrollView 触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!