在UITableViewCell中检测具有灵活宽度的视图的最终布局大小

Cli*_*rum 9 uitableview ios autolayout swift

我在iOS 8专用应用程序中使用大小类,我有一个UITableViewCell具有灵活宽度的视图.其自动布局约束定义如下:

在此输入图像描述

如您所见,其宽度取决于设备宽度/方向.

当我打印它的框架时,cellForRowAtIndexPath我看到(180.0,10.0,400.0,60.0)显示400宽度.但是当我在模拟器中测量视图时,它只有175宽,这是因为我的视图内容被截断(我在其中绘制了一些内容).

我怎么知道什么时候UITableViewCell的约束和子视图完全渲染渲染,以便我可以在我的视图中重绘东西?

更新

cellForRowAtIndexPath我做以下操作来获取单元格:

let cell = tableView.dequeueReusableCellWithIdentifier("TimeTypeCell", forIndexPath: indexPath) as! TimeTypeCell

let customField = customFields[indexPath.row]

cell.fieldNameLabel.text = customField["name"] as? String
cell.fieldValueLabel.text = customField["total"] as? String
cell.graphData = customField["totalGraph"] as! [Double]

cell.fieldGraph.dataSource = cell.self
cell.fieldGraph.delegate = cell.self

cell.fieldGraph.reloadData() //This is where the redrawing happens
Run Code Online (Sandbox Code Playgroud)

BJ *_*mer 20

调用cell.contentView.layoutIfNeeded()以正确布置内容视图.虽然单元格本身在返回时应该是正确的大小tableView.dequeueReusableCellWithIdentifier(_, forIndexPath:),但似乎内容视图不会立即运行布局,因此如果您在此时需要更新,则需要手动调用它.

  • 谢谢!!!!很棒的答案!我调用了这个 - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath (4认同)

mix*_*xel 15

我最终采用了重写UITableViewCell.layoutSubviews方法:

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.contentView layoutSubviews]; // this does the trick
}
Run Code Online (Sandbox Code Playgroud)