Ios 识别长时间点击的UITableViewCell的真正indexPath
我试图在集合视图中检测一个长点击的表视图单元格 我有我的主视图控制器(MainVC类),它包含一个CollectionView(CollectionViewController类)。集合视图中的每个单元格都有一个标签和一个TableView(TableViewController类)。 集合视图委托方法在MainVC中实现,而tableView委托方法在CollectionViewController类中实现 我试图在长时间点击时检测tableViewCell indexPath(以便获取其内容) 执行检测的代码(CollectionViewController内部): 系统识别出长敲击,但我遇到了一个问题。此代码:Ios 识别长时间点击的UITableViewCell的真正indexPath,ios,swift,uitableview,Ios,Swift,Uitableview,我试图在集合视图中检测一个长点击的表视图单元格 我有我的主视图控制器(MainVC类),它包含一个CollectionView(CollectionViewController类)。集合视图中的每个单元格都有一个标签和一个TableView(TableViewController类)。 集合视图委托方法在MainVC中实现,而tableView委托方法在CollectionViewController类中实现 我试图在长时间点击时检测tableViewCell indexPath(以便获取其内容
let indexPath = self.tableView.indexPathForRow(at: touchedPoint)
假设能够识别点击单元格索引XPath,似乎可以找到当前显示的点击单元格索引。我的意思是,如果设备当前没有显示上面的20个单元格,则indexPathForRow
将忽略它们,并仅统计显示的单元格。
因此,例如,如果在表视图上有30行,而用户当前正在查看第二十三行中的单元格,系统将考虑单元格在第四行中(例如),因为它是当前显示的第四个单元格。
我不知道是否与此tableView位于集合视图中这一事实有关联,但我希望此额外信息将帮助您帮助我(:
非常感谢!
我选择了Rowatindexpath
我想会得到您的帮助。请检查是什么,在使用它之前不要忘记委派tableView
var longpress = UILongPressGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
longpress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGestureRecognized))
tableView.addGestureRecognizer(longpress)
}
@objc func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
if longPress.state == UIGestureRecognizerState.began {
let locationInTableView = longPress.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: locationInTableView)
print(indexPath?.row ?? "-0")
}
}
我正在尝试检测长点击而不是常规点击。因此DidSelectRowatineXpath对我没有帮助。我弄错了吗?你必须将长点击识别器添加到自定义单元格中,这样做是肯定的。首先,我爱你。其次,问题确实出在这行:让locationInTableView=longPress.location(在:tableView中),我在self中检查了位置,而不是在tableView中。非常感谢!!!
var longpress = UILongPressGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
longpress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGestureRecognized))
tableView.addGestureRecognizer(longpress)
}
@objc func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
if longPress.state == UIGestureRecognizerState.began {
let locationInTableView = longPress.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: locationInTableView)
print(indexPath?.row ?? "-0")
}
}