我尝试过在 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/