问题描述
我有一个包含8个自定义单元格的表格视图.在第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触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!