I am using size classes in an iOS 8-only app and I have a view inside a UITableViewCell
that has a flexible width. Its auto layout constraints are defined as follows:
As you can see, its width varies depending on the device width/orientation.
When I print its frame in cellForRowAtIndexPath
I see (180.0, 10.0, 400.0, 60.0) which shows a 400 width. But when I measure the view in the simulator, it is only 175 wide, which is reinforced by the fact that my view's content is truncated (I'm drawing some stuff inside it).
How can I know when the UITableViewCell
's contraints and subviews are completely done rendering so that I can redraw stuff inside my view?
Update
In cellForRowAtIndexPath
I'm doing the following to get the cell:
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
Call cell.contentView.layoutIfNeeded()
to get the content views laid out correctly. While the cell itself should be the correct size when returned from tableView.dequeueReusableCellWithIdentifier(_, forIndexPath:)
, it seems that the content view doesn't immediately run layout, so you need to call it manually if you need it up to date at that point.
I ended up with overriding UITableViewCell.layoutSubviews
method:
- (void)layoutSubviews {
[super layoutSubviews];
[self.contentView layoutSubviews]; // this does the trick
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With