【发布时间】:2016-06-21 20:06:49
【问题描述】:
当您在表格视图中滚动时,它不会在您手指所在的单元格上开始表格委托方法 didSelectRowAtIndexPath 方法。如果我要点击该调用,而不是向上/向下滑动它,它会授予我具有委托方法的 indexPath。
我想知道在滚动 tableView 时是否可以获得手指所在单元格的 indexPath。这可能吗?
【问题讨论】:
当您在表格视图中滚动时,它不会在您手指所在的单元格上开始表格委托方法 didSelectRowAtIndexPath 方法。如果我要点击该调用,而不是向上/向下滑动它,它会授予我具有委托方法的 indexPath。
我想知道在滚动 tableView 时是否可以获得手指所在单元格的 indexPath。这可能吗?
【问题讨论】:
这可能是要走的路:
class TableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell")
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
}
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
let location = scrollView.panGestureRecognizer.locationInView(tableView)
guard let indexPath = tableView.indexPathForRowAtPoint(location) else {
print("could not specify an indexpath")
return
}
print("will begin dragging at row \(indexPath.row)")
}
}
【讨论】:
我猜你可以访问表格视图的panGestureRecognizer(因为它是一种滚动视图)并从中获取locationInView:。然后,您可以使用indexPathForRowAtPoint: 要求表格视图将其转换为索引路径。
【讨论】: