【问题标题】:indexPathForRowAtPoint.row is always returning zero for every cell in a tableViewindexPathForRowAtPoint.row 总是为 tableView 中的每个单元格返回零
【发布时间】:2015-03-19 09:25:33
【问题描述】:

我有 tableView 单元格,其中包含一些自定义视图。当自定义视图被触摸时,我需要存储被触摸视图所在的单元格的 indexPath.row。这就是我目前获取 indexPath 的方式:

@IBAction func bulletGroupPressed(sender: AnyObject) {

    let center = sender.center as CGPoint
    let indexPath = self.exercisesTableView.indexPathForRowAtPoint(center)!
    self.exercisesTableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)

    println(indexPath.row)
  }

我怀疑它与 indexPath 行上的 ! 有关,但我不确定它到底是什么。为什么 indexPath.row 总是在每个单元格上给我零?

【问题讨论】:

    标签: ios uitableview swift


    【解决方案1】:

    我不能快速给你一个编码的答案,但是这个点需要在表格的坐标系中说明。正如您所拥有的,中心在发送者父级的坐标系中表示,可能是表格视图单元格。使用convertPoint:toView:方法固定坐标系。

    或者,您可以考虑创建一个类似in my answer to this question 所示的方法。从任何视图(在您的情况下为发送者),它将沿着视图层次结构向上移动到第一个 UITableViewCell 并报告该单元格的索引路径。

    【讨论】:

    • 就是这样!谢谢!!
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 2023-03-05
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2013-06-03
    • 1970-01-01
    相关资源
    最近更新 更多