【问题标题】:Can't get convertRect: fromView to work无法让 convertRect: fromView 工作
【发布时间】:2016-11-17 18:37:59
【问题描述】:

在我的viewController 中,我有这个带有自定义单元格的tableView。 根据我的 viewController 的 坐标系,在我的 cellForRow:atIndexPath: 方法中,我想访问显示在特定位置的 UIButton 的位置 (rect)细胞。 这就是我认为它会起作用的方式:

func tableView(tableView: UITableView,
               cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? MyCell
    if cell != nil {

        cell = MyCell(style: .Default, reuseIdentifier: "cellId")
    }

    let convertedFrame = view.convertRect(cell!.myButton.Frame, fromView: cell)

    return cell!
}

它只是给我按钮在它自己的坐标系中的当前框架(0.0, 0.0, 44.0, 44.0)

关于正确使用该方法的任何线索?

【问题讨论】:

  • 您为什么要在cellForRowAtIndexPath 中这样做?该单元格甚至还没有添加到表格中。
  • 嗯。那讲得通。这可能是一个不好的地方。
  • 这不仅有意义,而且还成功了。非常感谢。

标签: ios swift uiview coordinates frame


【解决方案1】:

这只是在单元格实际放入视图层次结构之前调用它的问题。感谢 rmaddy 指出。

【讨论】:

    【解决方案2】:

    改变

    let convertedFrame = view.convertRect(cell!.myButton.Frame, fromView: cell)
    

    let convertedFrame = view.convert(cell!.myButton.Frame, fromView: cell)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2014-12-26
      • 2017-12-22
      • 2012-02-28
      • 2015-12-01
      • 2012-01-29
      相关资源
      最近更新 更多