ios - 如何在显示之前获取 TableView subview 的位置?

标签 ios swift uitableview uiview

我尝试过在 SO 上搜索此内容,但没有找到适合我的情况的东西。

这是我的层次结构和我想要做的事情:

//     ->  view
//     -> ->  myLabel           //I'd like to have this match center.x with...
//     -> ->  tableView
//     -> -> ->  tableViewCell
//     -> -> -> ->  UIStackView
//     -> -> -> -> ->  mySwitch    //...this, before everything appears

关于如何实现这一目标有什么建议吗?非常感谢所有帮助。谢谢!

<小时/>

编辑:此代码适用于定位,但在出现之前不会运行。它在必须加载/重用单元格时运行,因此它首先出现在错误的位置:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCellWithIdentifier("myCell") as? myCell {
        cell.configureCell(myArray[indexPath.row])
        let centerPoint = cell.mySwitch.convertPoint(cell.mySwitch.center, toView: self.view)
        myLabel.center.x = centerPoint.x - (myLabel.frame.width / 2)
        return cell
    } else {
       return myCell()
    }
}

最佳答案

我不确定我是否理解你的问题,但你可以尝试使用

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

您可以获得对单元格的引用并使用 cell.yourSwitch.frame.origin 来定位

关于ios - 如何在显示之前获取 TableView subview 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38161327/

相关文章:

ios - 如何在 hh :mm:ss format? 中输入时间值

ios - UIImagePickerController 的图像是否可能没有 CGImage 值?

ios - tableview 不想快速重新加载

swift - 如何将数据从 UICollectionViewCell(UITableviewCell 内部)传递到另一个 UIViewcontroller Swift3

ios - 使用 swift 更改 uitableView 特定单元格的标签文本

ios - NS小数乘法

ios - 导航栏中的分段控件

ios - 如何在 iOS 中创建类似 UIActionSheet 背景 View 的 View ?

objective-c - 使用 AutoLayout 在哪里添加我想在 NSScrollView 中 float 的 subview ?

ios - swift - 如何保存开关,即使我更改 TableViews