Define the size of the final layout for viewing with flexible width in UITableViewCell

I use size classes in an application for iOS 8 only, and I have a view inside a UITableViewCell that has a flexible width. Its automatic layout constraints are defined as follows:

enter image description here

As you can see, its width varies depending on the width / orientation of the device.

When I print its frame in cellForRowAtIndexPath , I see (180.0, 10.0, 400.0, 60.0) that shows a width of 400 . But when I measure the view in the simulator, it is only 175 wide , which is supported by the fact that the content of my view is truncated (I draw some things inside it).

How do I know when the UITableViewCell outlines and routines are fully executed so that I can redraw things inside my view?

Update

In cellForRowAtIndexPath I do 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 
+8
ios uitableview autolayout swift
source share
3 answers

Call cell.contentView.layoutIfNeeded() to properly display content views. Although the cell itself should be the right size when returning from tableView.dequeueReusableCellWithIdentifier(_, forIndexPath:) , it seems that the content view does not immediately launch the layout, so you need to call it manually if you need to update it at this point.

+13
source share

I ended up overriding the UITableViewCell.layoutSubviews method:

 - (void)layoutSubviews { [super layoutSubviews]; [self.contentView layoutSubviews]; // this does the trick } 
+11
source share

You need to either check the width in viewWillLayoutSubviews or viewDidLayoutSubviews .

If you put println(self.tableView.frame.width) in viewDidLoad , viewWillAppear , viewDidAppear , viewWillLayoutSubviews and viewDidLayoutSubviews , you can see the width when each method is called.

0
source share

All Articles